Edit. I asked the question but failed to make it so people understand what I am asking. So let's put some code in.
Parent component key parts:
constructor(props){
super(props);
...
this.state = {
hourMinute: 1200
}
}
.....
handleChange(hourMinute){
this.setState({hourMinute: hourMinute});
}
.....
<HourMinute onChange={this.handleChange} hourMinute={this.state.hourMinute} />
Child component key parts:
constructor(props){
super(props);
....
this.state = {
hour: 5, // 0..24
minute: 10, // 0..59
}
}
static getDerivedStateFromProps(props, state) {
const hourMinute = props.hourMinute;
const hour = Math.floor(hourMinute/60);
const minute = hourMinute - 60 * hour;
return {
hour: hour, // 0..24
minute: minute // 0..59
}
}
.......
handleChange(e){
this.setState({
[e.target.id]: e.target.value
});
if(valid) this.props.handleChange(hourMinute);
}
.......
<input id="hour" type="number" inputMode="numeric"
step="1" pattern="[0-9]*" maxLength="2" autoComplete="off"
className="form-control"
value={this.state.hour}
onChange={this.handleChange}/>
<input id="minute" type="number" inputMode="numeric"
step="1" pattern="[0-9]*" maxLength="2" autoComplete="off"
className="form-control"
value={this.state.minute}
onChange={this.handleChange}/>
As componentWillReceiveProps is depreciated and considered unsafe, and for getDerivedStateFromProps is explained that it should not be used in a way to copy received prop to state, what should be the right concept for this kind of situations?