I have a component that receives a variable as a prop. The getDerivedStateFromProps() is typical, meaning that when nextProps.someProp doesn't equal prevState.someStateVar, that prop is assigned to state variable.
I also have a Controlled element - an HTML text Input element which is associated with the same state variable.
The problem is, when I change the state variable through the controlled element, getDerivedStateFromProps() executes and sets the value back to the prop received earlier.
As a React novice, I don't understand why this happens. The above action should only occur when a new prop value is received, like the name 'nextProps' suggests.
Please suggest how to get the desired behaviour:
- Use prop to set initial state
- Let my controlled element (html input tag) set next values of the state variable
- iff new prop value is received, assign that to the next value of the state variable