React state management without the hassle.
React Magic State was created with simplicity and efficiency in mind. Its tiny API, comprised of just three methods, is enough to express all possible reactive state-component relationships.
The way React Magic State works is by wrapping your store(s) with ES6 proxies, and using them to keep track of every property that is accessed by your React components. Whenever an observed property changes, the relevant components are re-rendered by updating their internal state.
The use of ES6 proxies means that only current browsers are supported. This means no Internet Explorer support, sorry.
npm install --save react-magic-state
Simply wrap your store(s) with useStore()
:
const store = useStore({
foo: 42
});
Inside class components, you can call useStore()
to track local state:
const Hello = view(class Hello extends React.PureComponent {
constructor(props) {
super(props);
this.store = useStore({
name: "World"
});
}
render() {
return <h1>Hello {this.store.name}!</h1>;
}
});
Inside function components, you can call useStore()
to track local state:
const Hello = view(function Hello() {
const store = useStore({
name: "World"
});
return <h1>Hello {store.name}!</h1>
});
Wrap your components with view()
to track store(s) and react to changes:
const Hello = view(class Hello extends React.PureComponent {
render() {
...
}
});
You can also use view()
as a decorator with Babel or TypeScript:
@view
class Hello extends React.Component {
render() {
...
}
}
Wrap your bulk actions with batch()
to minimise re-renders and improve performance:
class Store {
bulkAction() {
batch(() => {
...
});
}
}