I want to use the same modal dialog for both editing and adding. I was previously using componentWillReceiveProps and setting the new state with props. However I read that it is being deprecated. I tried to use getDerivedStateFromProps but it behaves differently.
My old code was something like below
componentWillReceiveProps(nextProps) {
if (nextProps.original) { // Are we editing?
const item = nextProps.original;
this.setState({
name: item.name,
slug: item.slug
});
} else {
this.setState({ // Fresh
name: null,
slug: null
});
}
}
With above code, whenever the props change I was resetting the modal window with new state. Same code doesn't work for getDerivedStateFromProps My only solution is adding a key with String(new Date().getTime()) to my modal component so that each time I open the modal, it resets its state.
My Questions are;
- Should I keep using
componentWillReceivePropsif I am using "react": "16.8.3" with "react-native": "0.59.9" ? - Is it okay to reset my modal by using a key? If not what is a better way to reset it for add/edit?