Last active
July 15, 2017 11:41
-
-
Save andywer/7d4766a18eb10b36d6208bded7cf302a to your computer and use it in GitHub Desktop.
React Stateful (!) Functional Components
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
// counter.js | |
import stateful from 'new-fancy-statefulness' | |
const Counter = (props, state, { setState }) => ( | |
<div> | |
<div>Clicked {state.clicks} times</div> | |
<button onClick={props.onClick}>Increase +</button> | |
</div> | |
) | |
const increase = () => state => ({ clicks: state.clicks + 1 }) | |
export default stateful(Counter, { clicks: 0 }, { | |
onClick: event => ({ setState }) => setState(increase()) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There was a small mistake in the first version. Instead of correcting
onClick={setState(increase())}
toonClick={() => setState(increase())}
I eliminated the arrow function by adding aprops.onClick
handler.That should make it faster if the component gets re-rendered frequently :)