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
.
Source: stackoverflow.com
Related Query
- Change rendered HTML when state get updated React
- React useCallback does not get updated on state change
- State attribute doesn't get updated when route param changes in React
- Unable to get the updated state when using React useState hook
- How to get updated ID from array when calling set state in React
- Change state when properties change and first mount on React - Missing function?
- React shouldComponentUpdate() is called even when props or state for that component did not change
- How to check when the react components get rendered or rerendered
- React leaflet center attribute does not change when the center state changes
- Props not updating when redux state change in React Hooks
- how and when to call a react component methods after state change from redux
- Change the state when clicking outside a component in React
- React - useEffect running even when there was no change in state variable
- Content doesn't change when back/forward made on browser with state data in React + Redux
- React - How change state of single component when there are multiple instance of same component?
- React - change state right after previous state change was rendered
- how to get state right after state change in react hooks
- react setState doesn't change state even when I am using react update add on
- React setState componentDidUpdate React limits the number of .. when trying to update the state based on updated state
- React - change state when props are changed
- React component rendering even when there is no change in the state value
- Why React component get re-rendered only once when state does not change?
- When Redux state changes all components get updated values EXCEPT one
- React Component not updated when state changes
- React - why I still got same result when I already change the state
- how to auto refresh component when redux state get updated
- How to get the state updated of input field of google map api in React app
- REACT NATIVE - Change state of screen when close import modal
- React - handle change of input with state rendered by iteration through an array
- ResizeObserver API doesn't get the updated state in React
More Query from same tag
- Color picker from react-color doesn't work properly
- Updating state arrays with React Hooks
- Delete specific object with id
- How to use Google Cloud Storage with react.js?
- How to programmatically add to an array in database from frontend React
- Unable to install @mui/x-data-grid in react
- How to customize a div's scroll bar with css modules?
- Javascript event.target does not output as expected in ReactJs component
- How do I make this into 4 column in one row and also responsive as well?
- Why my Array find function doesn't work properly even I did the same thing document
- How to query for a list of results using geocoder API from Google?
- Are we allowed to use JSX outside of return in functional components?
- testcafe selector does not find 'div' elements
- adding plugin to forked ckEditor repo' not working
- When the form is empty, pressing the Login button opens the page. I have to make a mistake
- React Filter and Map data for Datagrid returning no values
- How to create loader spin for react/redux
- Initializing an object variable, Error: **'const' declarations must be initialized.**
- How to develop with Shopify's App Bridge while on localhost?
- How to display all the days of a particular time period in react-chartjs-2 X-axes
- Pass a laravel controller model to a react component
- Node, React, with concurrently, EADDRINUSE :::8000 error
- Get value of material UI buttons
- material-ui: input alters the tabs' this.state.value
- Custom Title in React.js
- React Redux unexpected key passed to create store
- Typescript --- Type 'string' is not assignable to type 'never'
- React give animation only to clicked element
- react native custom fonts
- What is the best way to import SVG using React.JS