Skip to content

Instantly share code, notes, and snippets.

@orYoffe
Last active July 25, 2020 09:23
Show Gist options
  • Save orYoffe/c7a1faa64e5f09255e0a8bdc95b8e971 to your computer and use it in GitHub Desktop.
Save orYoffe/c7a1faa64e5f09255e0a8bdc95b8e971 to your computer and use it in GitHub Desktop.
jstates_react_simple _example
import { createState, subscribe, useSubscribe } from "jstates-react";
const counterState = createState({ counter: 0 });
const addOne = () => counterState.setState(state => ({ counter: ++state.counter }));
const removeOne = () => counterState.setState(state => ({ counter: --state.counter }))
function Counter() {
return (
<>
<button onClick={addOne}>add one +</button>
<button onClick={removeOne}>remove one -</button>
</>
);
}
// with useSubscribe hook
function App() {
const { counter } = useSubscribe(counterState);
return (
<div>
<p>Current counter: {counter}</p>
<Counter />
</div>
);
}
export default App;
// with HOC subscribe function
function App({ counter }) {
return (
<>
<p>Current counter: {counter}</p>
<Counter />
</>
);
}
const mapStates = (counterState) => ({
counter: counterState.counter
});
export default subscribe(App, counterState, mapStates);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment