Last active
March 8, 2018 20:25
-
-
Save trueadm/ac676c26da9147c040ad064c7d0b3202 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
const state = React.createState({ | |
initialState(props) { | |
return { | |
counter: 0, | |
buttonRef: React.createRef(), | |
lastProps: props, | |
}; | |
}, | |
deriveState(props, state) { | |
return { | |
lastProps: props, | |
}; | |
}, | |
shouldUpdate(props, state) { | |
return shallowCheck(state.lastProps, props); | |
}, | |
didMount(props, state) { | |
// ... | |
}, | |
didUpdate(props, state) { | |
// ... | |
}, | |
willUnmount(props, state) { | |
// ... | |
}, | |
}); | |
function MyComponent(props) { | |
// sugar with a "::" before the name, to state we reading from it | |
const { state, setState } = <::State {...props} />; | |
return ( | |
<button onClick={reduceState(handleClick)} ref={state.buttonRef}> | |
Counter: {state.counter} | |
</button> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment