score:2
Accepted answer
setstate
is asynchronous. console logging synchronously right after setting state to see what that state looks like afterwards will most likely not reflect a correct value. if you want to see the value when total
changes, try something like this:
useeffect(() => {
console.log(total);
}, [total]);
Source: stackoverflow.com
Related Query
- setState for react function component not updated my state
- State is not getting updated in Function Component using useState in React
- React shouldComponentUpdate() is called even when props or state for that component did not change
- Using JS React component in TypeScript: JSX element type 'MyComponent' is not a constructor function for JSX elements
- React Arrow Function Component - setState is not defined
- React setState callback not called for external (npm packaged) component
- Axios not updating state from API in function called on Click, but works on identical function on Component Mount in React
- Waiting for react state to update in function component
- Component not mounted when setState called in arrow function in React with ES2016
- React hooks not returning updated state values inside callback function from library events (FabricJS)
- React Component not updated when state changes
- React hooks updated state not passed to function
- React Hooks setState function not setting state
- updated state value are not updated inside function in react
- React hooks: useState, Updater function nested in for loop not updating state
- Getting TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function in my React component and I'm not sure why
- React Dumb component Typescript error: Element is not a constructor function for JSX elements, property 'render' is missing
- React component not re-rendering when "Date" in state gets updated
- React Function Component Form not setting state variables?
- Why is this React component state recognized in the JSX return() for the first <p> but not for the second <p>?
- TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state
- React JS: previousState in setState is not keeping the data of the previous state after the state is updated
- State not updating for component in React
- Why callbacks in react functional component is not reading the updated state value
- react function not using updated state
- React component not updating after prop is updated using setState
- React test passes but component does not work properly when state updated in document.addListener handler
- Best practice for defining state in function component React
- React function component parent state change does not re-render child component
- React - state not getting updated after a prop being sent from another component
More Query from same tag
- Image source from database cannot display the image on react app
- Create a floor plan with React Native
- How to remove options in react quill
- React - How to pass returned data from an exported function to a component?
- Stenciljs Routing issue
- React Form with file submission
- WebStorm can't autocomplete styled-components
- setState not working in React JS
- React leaflet map location glitches when not in App.js
- Difference between React useCallback with pure function declared inside and outside component
- Uncaught TypeError: Cannot read property 'handleOptionChange' of undefined
- Need to integrate mxGraph with react js
- React Js Get Data with Fetch limit return
- Display json data in table (react, material.ui)
- react input component gets store overwritten onChange
- how to fix Background image not showing in react js
- Flux - How to initially load Store data with asynchronously fetched data
- How can I have an image that changes every 5 seconds in react.js?
- How i can save state ( data ) on Previous/Back" button click in ReactJS
- Idiomatic way to handle website closed/leave page events with React/React Router
- Vertical Stepper should scroll to top when changing steps in Material UI react
- How to create a union type with components?
- How to mock window.location.href with Jest in React?
- react-redux unable to dispatch action onchange of inputext
- CSS Modules export CSSes as <style> tag in head tag with disabled sourceMap
- How can I render an element when I clicked the function in ReactJS
- net::ERR_CONNECTION_REFUSED to cube.dv
- How to import file from outside /components in react
- check typeof graphql returned data
- React: In react class component, should I use !== to match nextState with currentState