score:1
Accepted answer
setting state in react is always async. that means history
will not be updated until the next render.
either you should pass squares
into didsomeonewin
and use it to determine a winner:
function squareclick(i){
// code
didsomeonewin(squares)
}
function didsomeonewin(squares) {
// remove line fetching squares from history
// code
}
or the call to didsomeonewin
should happen in a useeffect
that runs after each render:
useeffect(() => { didsomeonewin(); }, [history]);
// and add history as the dependency, so we only run `didsomeonewin` if history changes
Source: stackoverflow.com
Related Query
- Javascript Error: History always one step behind
- React Hooks state always one step behind
- Hooks setState always one step behind
- React state is always one step behind while making predictions on uploaded image using Tensorflowjs
- State retrieved from react-draft-wysiwyg is always one step behind
- State is always one step behind (setState async problems) - React js
- API call / state in an EventHandler is always one step behind - cant fix it with an useEffect
- React context state updates are always one step behind
- How to solve setState is always one step behind on React JS
- React state always one step behind when getting cookie
- React form onChange->setState one step behind
- ReactJS onClick state change one step behind
- ReactJS state updates one step behind
- Loading data asynchronously and downloading CSV data with a click is one step behind the latest retrieved data
- React Hooks: component re-renders one step behind on state update
- react hooks setState lagging one step behind
- useState value is one step behind in reactjs
- 3rd dropdown is based off first 2 dropdown's selections but the 3rd's state is always 1 step behind
- React Hooks state always a step behind
- Key error when there is already one on Javascript and REACT AXIOS
- React state one step behind after set state
- Problem with react useState falling behind one step
- Child React Component Always One Update Behind
- React js rendered value of state is one step behind
- Dropdown select component state one step behind
- How to fix state change issue which work one step behind onClick event
- React State Rendering one step behind
- Component updates state from one step behind in react
- Async data retrieval via input box is one step behind
- useState hook always 1 step behind
More Query from same tag
- React useState Causing an infinite loop
- Getting warnings when implementing Facebook Pixel
- Redux: Add/remove from nested object/array, without mutating?
- Recommended approach for route-based tests within routes of react-router
- Router not navigating to next page but url changes
- props.history.push('/') does not go to top of page (need functional component)
- What can I use in functional components to have same behavior as componentDidMount?
- Stale closure in custom hook
- Cannot read properties of created ref with createRef()
- Converting Blob WebM to audio file (WAV or MP3)
- Passing function down multiple children reactJS
- Unexpected output using react-router-dom with React's Context API
- How to convert Array of string to List of string in react js
- Unexpected use of 'confirm' no-restricted-globals
- How to send from node data to localStorage React?
- How To Query Firestore to get Document by String Field (Search Engine Friendly Slug)
- How to export a variable to make ti dynamic using React Redux
- Dynamic API routing with getServerSideProps in Nextjs
- Suddenly React cannot execute the 'create-react-app' command. Why is this happening and how can I solve it?
- React mapping nested data
- Abort Controller in IE11 doesn't work. Any way to support it
- How to let Query are performed at the component onmount and triggered by user event later?
- Display html content in iframe via props in React
- How to fetch api from the function which pushes each api in array?
- How to use yarn to create a React app project?
- Deploy React with Nginx failed to load resources
- After using react-router the onClick event is not firing the function in first time and works fine after that
- Asp.net Core: Cross-Origin Request Blocked
- Why do I get an error TS2339: Property 'classList' does not exist on type 'never'?
- Why can't Next.js import files under a folder