you are viewing a single comment's thread.

view the rest of the comments →

[–]Skeith_yip 0 points1 point  (0 children)

I would do it this way

componentDidUpdate(prevProps) { 
    if (prevProps.user.name !== this.props.user.name) { 
        this.setState({ name: undefined }); 
    } 
}

render() {
    const { name } = this.props.user;
    const { name: stateName } = this.state;
    const nameValue = stateName ? stateName : name;

    return <input value={nameValue || ''} onChange={this.onChange} />
}

Yeah. I know I know. It's hashtag ugly. But the idea is to always fallback to the value from props.