Contextual state management for React, ReasonReact and Preact
MIT License
Bot releases are visible (Hide)
Published by lxsmnsyc almost 5 years ago
.Provider
and be received by the model hook itself.Hook:
const Counter = createModel(function ({ initialCount }) {
const [count, setCount] = useState(initialCount);
const increment = useCallback(() => {
setCount(c => c + 1);
}, []);
const decrement = useCallback(() => {
setCount(c => c - 1);
}, []);
return {
count,
increment,
decrement,
};
});
on Provider:
export default function App() {
return (
<Counter.Provider initialCount={1}>
<Counter.Provider initialCount={0}>
<Count />
<Increment />
<Decrement />
</Counter.Provider>
<Count />
<IncDec />
</Counter.Provider>
);
}
Published by lxsmnsyc almost 5 years ago
useProperties
which allows users to listen to the changes of multiple properties.function IncDec() {
const [increment, decrement] = Counter.useProperties(['increment', 'decrement']);
return (
<React.Fragment>
<button type="button" onClick={increment}>Increment</button>
<button type="button" onClick={decrement}>Decrement</button>
</React.Fragment>
);
}