简体   繁体   English

setState 不会在 React Js 中更新数组 state

[英]setState does not update array state in React Js

I try to get value from the input and put it into object then push to new array.我尝试从输入中获取值并将其放入 object,然后推送到新数组。 Then combine this array(with setState) with my state array.然后将这个数组(使用 setState)与我的 state 数组结合起来。 However, my state array value always returns empty.但是,我的 state 数组值总是返回空值。 Could you please say where is the mistake.能不能说一下错在哪里。 Thanks.谢谢。

class AddExtraSeassion extends Component {
  constructor(props) {
    super(props);

    this.checkValidity = this.checkValidity.bind(this);
    this.onChangeDate = this.onChangeDate.bind(this);
    this.onChangeTime = this.onChangeTime.bind(this);
    this.onChangeDuration = this.onChangeDuration.bind(this);

    this.state = {
      checkedStudentLength: 0,
      validity: false,
      seassionDuration: null,
      seassionDate: null,
      seassionTime: null,
      showSeassion: false,
      seassions: []

    }
  }


  addSeassion = () => {
    this.setState({showSeassion: true});
  }

  onChangeDate = (event) => {
    this.setState({seassionDate: event.target.value});
  };
  onChangeTime = (event) => {
    this.setState({seassionTime: event.target.value});
  };
  onChangeDuration = (event) => {
    this.setState({seassionDuration: event.target.value});
  };

  checkValidity() {
    const seassionDate = this.state.seassionDate;
    const seassionTime = this.state.seassionTime;
    const seassionDuration = this.state.seassionDuration;

    const obj = {
      "Date": seassionDate,
      "Time": seassionTime,
      "Duration": seassionDuration
    };
    let SeassionList=[];

    SeassionList.push(obj);
    console.log(JSON.stringify(SeassionList) + " SeassionList array"); // print result
    this.setState({
      seassions: [...this.state.seassions, SeassionList]
    })
    console.log(JSON.stringify(this.state.seassions) + " state array"); // always empty

As @norbitrial mentioned setState is async operation so console.log() will not show the updated state. If you want to check if the state is updating or not you can use the callback provided by the setState method like below:正如@norbitrial 提到的 setState 是异步操作,所以 console.log() 不会显示更新的 state。如果你想检查 state 是否正在更新,你可以使用 setState 方法提供的回调,如下所示:

this.setState(prevState => ({
      seassions: [...prevState.seassions, SeassionList[0]]
    }),() => {
    console.log(this.state.seassions);
})

The first thing is you should not expect to see the changes in that line of console.log() immediately after your usage of setState() because it is asynchronous action.首先,您不应期望在使用setState()后立即看到console.log()行中的更改,因为它是异步操作。

Also you can try using the previous state of you array and passing obj instead of SeassionList :您也可以尝试使用数组的前一个 state 并传递obj而不是SeassionList

this.setState(prevState => ({
   ...prevState,
   seassions: [...prevState.seassions, obj]
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM