Last active
July 25, 2020 09:23
-
-
Save orYoffe/c7a1faa64e5f09255e0a8bdc95b8e971 to your computer and use it in GitHub Desktop.
jstates_react_simple _example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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