GeekEast / react-props-key

Reset children component state when some parts of parent components changes

Home Page:https://codesandbox.io/s/github/GeekEast/react-props-key

Repository from Github https://github.comGeekEast/react-props-keyRepository from Github https://github.comGeekEast/react-props-key

Needs

  • Parent components pass default value of state into children component
  • state management is handled by the children component ( uncontrolled )
  • when some parts of state in parent component changes, the children component can be reset

key

  • simply key=<some_parts_of_state_in_parent>

When a key changes, React will create a new component instance rather than update the existing one.

class EmailInput extends Component {
  state = { email: this.props.defaultEmail };

  handleChange = event => {
    this.setState({ email: event.target.value });
  };

  render() {
    return <input onChange={this.handleChange} value={this.state.email} />;
  }
}

<EmailInput defaultEmail={this.props.user.email} key={this.props.user.id} />;

About

Reset children component state when some parts of parent components changes

https://codesandbox.io/s/github/GeekEast/react-props-key


Languages

Language:HTML 59.4%Language:TypeScript 38.3%Language:CSS 2.2%