Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

getderivedfromstate alternative

class PartiallyControlledInput extends React.Component {
  state = {
    value: this.props.value,
  };  componentWillReceiveProps(nextProps) {
    if (nextProps.value !== this.props.value) {
      this.setState({ value: nextProps.value });
    }
  }  render() {
    return (
      <input
        onChange={(e) => this.setState({ value: e.target.value })}
        value={this.state.value}
      />
    );
  }
}
Source by levelup.gitconnected.com #
 
PREVIOUS NEXT
Tagged: #getderivedfromstate #alternative
ADD COMMENT
Topic
Name
1+5 =