Skip to content

Instantly share code, notes, and snippets.

@nosovsh
Last active May 23, 2016 08:58
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 nosovsh/bfc2e04686c391056cc2377f0ab4ff30 to your computer and use it in GitHub Desktop.
Save nosovsh/bfc2e04686c391056cc2377f0ab4ff30 to your computer and use it in GitHub Desktop.
// Two states can live together in one component.
// `this.state` is a React local state. Can be changed using `this.setState()`
// `this.props.state` is a slice of Redux state. Can be changed using `this.props.setState()`
class MyFormVanilla extends React.Component {
constructor(props) {
super();
// Default value for local React state
this.state = {
checked: false,
};
}
render () {
return (
<div>
This input is controlled by local React state
<input
type="checkbox"
checked={this.state.checked}
onChange={e => this.setState({'checked': !this.state.checked})}
/>
This Input is cotrolled by Redux state stored by 'path.to.your.form.in.redux.state' path
<input
type="checkbox"
checked={this.props.state.checked}
onChange={e => this.props.setState({'checked': !this.props.state.checked})}
/>
</div>
)
}
}
MyFormVanilla.defaultProps: {
// Default value for slice of Redux state
state: {
checked: false
}
}
export default connectSlicedState('path.to.your.form.in.redux.state')(MyFormVainlla);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment