Skip to content

Instantly share code, notes, and snippets.

@trueadm
Last active March 8, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueadm/ac676c26da9147c040ad064c7d0b3202 to your computer and use it in GitHub Desktop.
Save trueadm/ac676c26da9147c040ad064c7d0b3202 to your computer and use it in GitHub Desktop.
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