我从数据库获取值并将其保存为状态 enteredEvent :
class FormEditPage extends Component {
constructor(props) {
super(props);
this.state = {
enteredEvent: {
name: '',
date: new Date(),
time: '',
place: '',
datepub: new Date()
},
};
...
}
componentDidMount() {
this.onHandleEventOneFetch(idEvent);
}
handleChangeInputName(newValue) {
this.setState({ enteredEvent: { name: newValue } });
}
handleChangeInputDate(newValue) {
this.setState({ enteredEvent: { date: newValue } });
}
handleChangeInputTime(newValue) {
this.setState({ enteredEvent: { time: newValue } });
}
handleChangeInputPlace(newValue) {
this.setState({ enteredEvent: { place: newValue } });
}
handleChangeInputDatepub(newValue) {
this.setState({ enteredEvent: { datepub: newValue } });
}
onHandleEventOneFetch(id) {
fetch(..., {
method: 'GET'
})
...
.then(data =>
this.setState({
enteredEvent: {
name: data[0].name,
date: new Date(data[0].date),
time: data[0].time,
place: data[0].place,
datepub: new Date(data[0].datepub)
}
})
);
}
render() {
return (
<div>
<FormEvent
enteredEvent={this.state.enteredEvent}
onHandleChangeInputName={this.handleChangeInputName}
onHandleChangeInputDate={this.handleChangeInputDate}
onHandleChangeInputTime={this.handleChangeInputTime}
onHandleChangeInputPlace={this.handleChangeInputPlace}
onHandleChangeInputDatepub={this.handleChangeInputDatepub}
/>
</div>
);
}
}
在此组件中,我添加了数据选择器和时间选择器:
import DatePicker from 'react-date-picker';
import TimePicker from 'react-time-picker';
class FormEvent extends Component {
constructor(props) {
super(props);
this.handleNameChange = this.handleNameChange.bind(this);
this.handleDateChange = this.handleDateChange.bind(this);
this.handleTimeChange = this.handleTimeChange.bind(this);
this.handlePlaceChange = this.handlePlaceChange.bind(this);
this.handleDatepubChange = this.handleDatepubChange.bind(this);
}
handleNameChange(event) {
this.props.onHandleChangeInputName(event.target.value);
}
handleDateChange(newDate) {
this.props.onHandleChangeInputDate(newDate);
}
handleTimeChange(newTime) {
this.props.onHandleChangeInputTime(newTime);
}
handlePlaceChange(event) {
this.props.onHandleChangeInputPlace(event.target.value);
}
handleDatepubChange(newDatepub) {
this.props.onHandleChangeInputDatepub(newDatepub);
}
render() {
return (
<div>
<input type='text' required value={this.props.enteredEvent.name} onChange={this.handleNameChange}/>
<DatePicker onChange={this.handleDateChange} value={this.props.enteredEvent.date}/>
<TimePicker onChange={this.handleTimeChange} value={this.props.enteredEvent.time}
<input type='text' value={this.props.enteredEvent.place} onChange={this.handlePlaceChange}/>
<DatePicker onChange={this.handleDatepubChange} value={this.props.enteredEvent.datepub}/>
</div>
);
}
FormEvent.propTypes = {
enteredEvent: PropTypes.object,
onHandleChangeInputName: PropTypes.func,
onHandleChangeInputDate: PropTypes.func,
onHandleChangeInputTime: PropTypes.func,
onHandleChangeInputPlace: PropTypes.func,
onHandleChangeInputDatepub: PropTypes.func
};
}
结果,所有日期选择器和时间选择器都从enterEvent获得值。当我更改日期选择器/时间选择器之一中的值时,其他日期选择器和时间选择器中的值变为 null 。我该如何解决?
答案 0 :(得分:2)
您已经将状态设为嵌套对象,并且在设置状态时,您将覆盖整个对象。您将需要合并具有先前状态的对象,或者停止使用嵌套对象。 React将进行状态的浅表合并,但不会进行深层合并。
如果您可以使用对象传播语法,请自行进行合并:
this.setState(oldState => ({
enteredEvent: {
...oldState.enteredEvent,
name: newValue
}
});
如果您不可以使用对象传播语法,则可以执行以下操作:
this.setState(oldState => ({
enteredEvent: Object.assign({}, oldState.enteredEvent, {name: newValue})
});
如果您想采用扁平化状态的方法,它将看起来像这样:
this.state = {
name: '',
date: new Date(),
time: '',
place: '',
datepub: new Date()
};
// ...
this.setState({ name: newValue });
答案 1 :(得分:1)
当嵌套对象处于状态时,必须确保创建当前状态的对象的副本,否则它将被仅具有给定属性的新对象覆盖。
示例
handleChangeInputName(newValue) {
this.setState(previousState => ({
enteredEvent: { ...previousState.enteredEvent, name: newValue }
}));
}