score:0

you should always update your state by using this.setstate({...this.state.flightdetails, flightdetails}); inside the handleadddetails() function. by using setstate the rerender will be triggered correctly.

score:1

you can't update state as you show at an example. you need to use setstate to update your state.

you should do this like this:

  handleadddetails = flightdetails => {
    this.setstate(prevstate => ({
      flightdetails: [...prevstate.flightdetails, flightdetails]
    }));
  };

and when you want to add some console.log's to check that your state changed properly you can do this like that:

  handleadddetails = flightdetails => {
    this.setstate(
      prevstate => ({
        flightdetails: [...prevstate.flightdetails, flightdetails]
      }),
      () => {
        console.log(`${this.state.flightdetails.length} ${flightdetails}`);
      }
    );
  };

by using console.log in the setstate function you can be sure that console.log will show correct value after adding new elements or changing the value of state.


Related Query

More Query from same tag