score:0
Accepted answer
It is due to this fact that state
always updates after rerendering, so when you pass it to upper component, it still has its previous value!
To solve this problem you have two solutions:
- update your code as follow:
const listChangeHandler = (e) => {
e.preventDefault();
let temp = [...ingredientList, ingredient];
setIngredientList(temp);
inputIngredient.current.value = "";
props.data(temp);
};
- use
useRef
instead ofuseState
because in contrast tostate
,ref
updates imediately.
Source: stackoverflow.com
Related Query
- Passing data from child to parent after Array State has been updated, Why am I getting an instance of the prev state?
- Can't get React component to re-render after state has been updated with returned data from Parse Server
- How to test state that has been lifted up from child to parent component in React?
- Passing data from child to parent and setting state
- Issues when passing data from parent to child component right after fetching data from API. (functional components only)
- reactjs passing array of objects from parent state by props to child
- Passing state from child to parent in React; child has separate state
- Passing data from parent to child using react.State but State reset to initial state
- Can't change State from child component even after passing a function from parent component
- How to get state data right after changing them from parent to child in React?
- Even after passing updated value from parent to child, child is not rendered
- Passing updated state from parent to child
- passing data from the parent component state to the child component using React.createContext
- Passing state as props from Parent to Child in React-Router?
- passing data from child to parent component - react - via callback function
- Why is child component not receiving updated value from parent in React Native?
- Passing state from a child to parent component
- Passing API data from parent container to child component in React
- Get data from Redux Store after data has been added
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- Prevent child's state from reset after parent component state changes also get the values of all child components:ReactJS+ Typescript
- Why can't I see parent state from child component event handler?
- Passing state from child class to parent
- React - Passing state from child to parent in drop down select
- React.js: Parent state values not passing into child properties + Fetch API data cannot be accessed
- React router dom passing data from parent component to child router component does not pass the props.match
- Why state doesn't keep all the data in Array state in hook when it updated in foreach?
- Updating Parent component state from multiple child components not taking updated state value into account
- Why does the useEffect() seem to reset my local state after receiving data from a socket?
- Why passing a function from a parent Class Component to a child Functional Component doesn't work?
More Query from same tag
- axios doesnt post data to server
- Uploading a file from React to S3 Gives CORS errors
- React nested map sequential render with setTimeout
- Formik arrayHelpers remove is not removing the selected item
- How to render programmatically created table in React?
- cancel event on input type="file" hidden
- Update Display state when App state changes
- i am getting the following error while installing material-UI in vs code with npm WHAT SHOULD I DO?
- refreshing / maunally typing url gives blank page - React router dom 6
- Location me in map from React Leaflet
- useLoopCallback -- useCallback hook for components created inside a loop
- React router, pass props with Link
- Dynamically updating a highcharts object after data refresh in React
- Toggling a CSS class when a React.js component gets updated
- How is changed state supposed to be re rendered exactly in redux relative to flux?
- Calling a function when the datepicker closes, when I click only outside of the datepicker
- How to hide a label for Autocomplete on onFocus in Material-ui?
- React passing down hooks causes rerender and loss of focus on input
- Stale custom hook state property on callback
- How to set only ONE element in an array in firebase
- How to add Event Listeners to UseRefs within UseEffect
- Remove brackets from JSON array React
- getStaticPaths not creating paths
- React: How to get submit button and text label to communicate with each other?
- React hook not updating on click
- Change object structure Javascript
- Dynamically use React components with strings
- The value in the INPUT field cannot store in state of React
- Count time between 2 dates for multiple forms using reactjs
- React Bootstrap Alert Default true