I'm trying to find a more React-type way of changing the input field when the state changes after a user input. This is my current setup, but I'm looking for a way to do this without signaling to the DOM in the componentWillReceiveProps method:
export default class Example extends React.Component {
constructor(props){
super(props);
this.state = {title: props.arr.question};
};
componentWillReceiveProps(nextProps){
if(nextProps.arr.question !== this.state.title){
let id = "question" + this.props.arr.key;
document.getElementById(id).value = nextProps.arr.question;
this.setState({title: nextProps.arr.question});
}
}
render(){
return(<div>
<input type="text" id={"question" + this.props.arr.key} defaultValue={this.state.title} placeholder="Enter your title."/>
</div>
)
}
}
What I assumed was that when the state changes, I will see the input change as well. In fact, that happens for any element except input fields, for some reason. So the only think that I found works is referencing the DOM in the componentWillReceiveProps method and change it like that.
Is there a better way to do this that I'm unaware of?