Created
April 18, 2018 00:02
-
-
Save idmontie/195cfc7143bbd6e4907d5a08caaf86c8 to your computer and use it in GitHub Desktop.
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
class App extends Component { | |
stateReducer = (prevState, nextState) => { | |
switch (nextState.type) { | |
case Toggle.TOGGLE: { | |
const count = prevState.count || 0; | |
const isOn = count < 4 ? nextState.on : false; | |
return { | |
...prevState, | |
...nextState, | |
count: isOn ? count + 1 : count, | |
on: isOn, | |
}; | |
} | |
default: | |
return nextState; | |
} | |
} | |
render() { | |
return ( | |
<Toggle stateReducer={this.stateReducer}> | |
{({ on, onToggle, count }) => ( | |
<Fragment> | |
<Switch on={on} onToggle={onToggle} /> | |
{count} | |
</Fragment> | |
)} | |
</Toggle> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment