为什么当我更改其中一个时所有日期选择器值都变为空值?

Why all datepicker values become null when I change one of this?

我从数据库中获取值并将其保存在状态 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
};
}

结果所有日期选择器和时间选择器都从 enteredEvent 获取值。当我更改 datepicker/timepicker 之一的值时,其他日期选择器和时间选择器中的值变为 null。我该如何解决?

当您有一个处于状态的嵌套对象时,您必须确保创建当前处于状态的对象的副本,否则它将被仅具有给定 属性.[=11= 的新对象覆盖]

例子

handleChangeInputName(newValue) {
  this.setState(previousState => ({
    enteredEvent: { ...previousState.enteredEvent, name: newValue }
  }));
}

您已将您的状态设置为嵌套对象,当您设置状态时,您将覆盖整个对象。您将需要将该对象与之前的状态合并,或者停止使用嵌套对象。 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 });