0.1.5 • Published 1 year ago
ryze v0.1.5
Ryze
A minimal state management library for React.
Install
npm install ryzeExample
import {createStore} from 'ryze';
const {store, useSlice} = createStore({count: 10, todos: []});
const Counter = () => {
const count = useSlice('count');
return (
<div>
<div>count: {count}</div>
<button
onClick={() => {
store.setState((prev) => ({...prev, count: prev.count + 1}));
}}
>
Add
</button>
</div>
);
};
const getActiveTodos = (state) => state.todos.filter((item) => !item.completed);
const Todos = () => {
const todos = useSlice(getActiveTodos);
return (
<div>
<ul>
{todos.map((todo) => (
<li key={todo.date}>{todo.title}</li>
))}
</ul>
<button
onClick={() => {
store.setState((prev) => ({
...prev,
todos: [...prev.todos, {title: 'New Todo', date: +new Date()}],
}));
}}
>
Add
</button>
</div>
);
};
const Example = () => {
return (
<>
<Counter />
<Todos />
</>
);
};Constraints
Selectors
Selector passed to useSlice should have the same identity across re renders.
That is,
- either declare selectors outside of components, or
- if the selector is dependent on component props, use
useCallbackto ensure the selector only changes when the prop change.
Updates
Updates should be immutable. Return new values, rather than modify the state value directly.
0.1.4
1 year ago
0.1.3
1 year ago
0.1.5
1 year ago
0.1.2
2 years ago
0.1.0
2 years ago
0.1.1
2 years ago
0.0.10
3 years ago
0.0.11
3 years ago
0.0.12
3 years ago
0.0.1
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.9
3 years ago
0.0.8
3 years ago
0.0.5
3 years ago
0.0.4
3 years ago
0.0.7
3 years ago
0.0.6
3 years ago
0.0.0
6 years ago