0.3.1 • Published 4 years ago
react-slice v0.3.1
React Slice
A simple performant approach to state management in React.
- Uses reducers and dispatch 🤖
- Access state from anywhere with hooks 🚀
- You can still use statically 😲
- Optimized for use with Typescript 💙
- Full control of render performance 🔥
- 2kb minified 💪
Demo
I've made a few Codesandboxes to play around with react-slice
Installation
npm i react-slice
The basics
Create a file and export your hook w/ reducer (fx counterSlice.js)
// counterSlice.js
import { createSlice } from 'react-slice'; // 👈👈👈
export default createSlice({
reducer: /*👈👈👈*/ (state, action) => {
switch (action.type) {
case 'increment':
return state + 1;
default:
return state;
}
},
initialState: /*👈👈👈*/ 0,
debugName: 'Counter', // Optional.
});
Then import that file somewhere else (fx App.jsx) 🏴☠️
// App.jsx
import React from 'react';
import counterSlice from './counterSlice'; // 👈👈👈
export default function App(test) {
const counterState = counterSlice.use(); // 👈👈👈
const onClick = () => {
counterSlice.dispatch({ type: 'increment' }); // 👈👈👈
};
return (
<div className="App">
<h1>{counter /*👈👈👈*/}</h1>
<button onClick={onClick}>Increment</button>
</div>
);
}
And boom! 💥 That's all you need to get up and running!
Advanced/FAQ
- API for createSlice()
- Avoid unnecessary re-renders
- Use statically from utils etc.
- Use Typescript and get strongly typed state and actions 💙
- Use with Server-side rendering (coming soon, would love ideas/usecases)
- Debugging is disabled when
process.env
is 'production' or 'test'
0.3.0
4 years ago
0.3.1
4 years ago
0.2.4
4 years ago
0.2.3
4 years ago
0.2.2
5 years ago
0.2.1
5 years ago
0.2.0
5 years ago
0.1.8
5 years ago
0.1.7
5 years ago
0.1.6
5 years ago
0.1.5
5 years ago
0.1.4
5 years ago
0.1.3
5 years ago
0.1.2
5 years ago
0.1.1
5 years ago
0.0.12
5 years ago
0.0.11
5 years ago
0.0.10
5 years ago
0.0.9
5 years ago
0.0.8
5 years ago
0.0.7
5 years ago
0.0.6
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago