-
-
Save naartjie/aa3f2e5f80721ca288025aa6f1f48b85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Option A: | |
| const React = require('react'); | |
| const counterState = React.createState(); | |
| const counterReducer = (action, state) => { | |
| switch (action) { | |
| case "INCREMENT": { | |
| return React.updateState({counter: state.counter + 1}); | |
| } | |
| } | |
| }; | |
| function CounterComponent(props) { | |
| return counterState({ | |
| initialState: () => ({ | |
| counter: 0, | |
| divRef: React.createRef(), | |
| }), | |
| didUpdate: state => { | |
| state.divRef.style.color = props.color; | |
| }, | |
| reducer: counterReducer, | |
| render(state, reduce) { | |
| return ( | |
| <div ref={state.divRef} onClick={() => reduce("INCREMENT"))}> | |
| Count: {state.counter} | |
| </div> | |
| ); | |
| } | |
| }); | |
| } | |
| module.exports = CounterComponent; |
This file contains hidden or 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
| // Option B: | |
| const React = require('react'); | |
| const counterState = React.createStateReducer((action, state) => { | |
| switch (action) { | |
| case "INCREMENT": { | |
| return React.updateState({counter: state.counter + 1}); | |
| } | |
| } | |
| }); | |
| function CounterComponent(props) { | |
| return counterState({ | |
| initialState: () => ({ | |
| counter: 0, | |
| divRef: React.createRef(), | |
| }), | |
| didUpdate: state => { | |
| state.divRef.style.color = props.color; | |
| }, | |
| render(state, reduce) { | |
| return ( | |
| <div ref={state.divRef} onClick={() => reduce("INCREMENT"))}> | |
| Count: {state.counter} | |
| </div> | |
| ); | |
| } | |
| }); | |
| } | |
| module.exports = CounterComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment