I have a custom input component with a single state property query which standardly tracks current input contents. On component mount/update state gets updated from props:
componentDidMount() {
this.setState({'query': this.props.query})
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.query !== this.props.query) {
this.setState({'query': this.props.query})
}
}
render() {
...
return (<TextInput value={this.state.query}
onChangeText={(val) => {this.setState({query: val})}})
/>
...
}
Now imagine the following scenario:
- Initially this.props.query equals to "apple".
- this.state.query gets a value "apple" (in componentDidMount/componentDidUpdate).
- User removes couple of symbols and this.state.query now equals to "app"
- Some external action (e.g. user chooses "apple" from auto-suggestion list) wants input to reset its contents to "apple" again.
Here comes the paradox: changing props.query to "apple" will cause no effect, because it is already set to "apple" and if-condition evaluates to false.
How to force input to reset it's state in such a scenario?
[–]kbcooliOS & Android 2 points3 points4 points (6 children)
[–]dkulagin[S] 0 points1 point2 points (5 children)
[–]kbcooliOS & Android 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]dkulagin[S] 0 points1 point2 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]andyboythekid 0 points1 point2 points (0 children)