accountId won't have been updated this render. You have to wait for the next render for it to be updated. accountId only gets populated at the top of the function component when useState is called. You're in the middle of the render. If you need the actual value, keep pulling it out of e.currentTarget.value.
reactjs - React useState update based on a variables previous value - Stack Overflow
reactjs - state (from useState) using old value on a callback - Stack Overflow
console log showing previous number used on useState
How does Javascript/React know to pass in the previous value of a stateful variable?
How to Update State Based on Previous State?
The Role of useEffect in Tracking State Changes
Videos
accountId won't have been updated this render. You have to wait for the next render for it to be updated. accountId only gets populated at the top of the function component when useState is called. You're in the middle of the render. If you need the actual value, keep pulling it out of e.currentTarget.value.
From react docs
React may batch multiple setState() calls into a single update for performance.
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
State updates may be batched and updated asynchronously, therefore the state may have not updated when console.log() is called. You will get the guaranteed updated result in your next call to useEffect hook.
I am new to react const [ state, setState] = useState(0)
when I press a button that uses function such as
const setAction = () => () => {setState(randomNumber) // 5 > 6> 3 > 2 console.log(state) //prints 0 then 5 > 6 > 3 > 2
it's not showing the desired number and is lagging one step behind... am I doing something wrong?
When studying react and first learning the useState hook, I came across a scenario where I had to update state based on the previous value of the stateful variable.
The original code was:
const [state, setState] = useState('')
and the code in the event handler function was:
setState(prev => prev + 1)
My main question is in situations in Javascript like this where we pass a variable/parameter into a function, how does the computer know that 'prev' is the previous value of the stateful variable? There are a lot of situations like this where I am passing a variable into a function but I dont understand how the computer knows the exact variable I want to pass so I'd like to know how to tell exactly what is passed into a function. Thanks in advance.
I am little confused how react state works in react functional components.
In the below code i am trying to update the count state with new value and then with the previous value in the same function when i click on the button.
And i observe that my function is rerendered in this case and and with this i am updating my temp variable with a random number. But i didn't see the change in the value of temp in the DOM regardless the temp value is changing .
import React, { useEffect, useState } from 'react'
let temp;
function App() {
console.log("rendering")
const [count, setCount] = useState(0)
temp=Math.random(0,1);
console.log("temp value is :",temp)
function handleCount(){
setCount(1)
setCount(0)
}
return (
<div>
<h2>Count value is:{count}</h2>
<button onClick={handleCount}>Click me</button>
<p>temp value is :{temp}</p>
</div>
)
}
export default AppOutput in console:-
Initial Render: Rendering temp value is : 0.309
1st button click: Rendering temp value is : 0.257
2nd button click: Rendering temp value is : 0.951
Output in UI is same in each button click:-
Count value is: 0
temp value is : 0.309
My question is simple that why my temp value is not changing in the dom regardless my component is rerenderd in every button click .
Can anyone tell me how useState works and specially for the above code.
The useState hook is somewhat asynchronous (although you cannot wait for it).
Try using a useEffect:
useEffect(() => {
console.log(isEnabled)
}, [isEnabled]) // Array of dependencies: when any of these value changes, the function in the useEffect will re-run
More information here:
- https://dev.to/shareef/react-usestate-hook-is-asynchronous-1hia
- https://javascript.plainenglish.io/why-you-shouldnt-always-use-usestate-658994693018
The Change function will always "see" the state value that existed at the time of running the function. This is not because of asynchronicity per se (state updates are actually sync) but because of how closures work. It does feel like it is async though.
The state value will properly update in the background, but it won't be available in the "already-running" function. You can find more info here.
The way I see your handler implemented though:
const handleChange = () => {
setIsEnabled(!isEnabled) // you do not need updater function, you can directly reference the state
triggerParentMethod(!isEnabled); // then you can also directly call the parent function here
}
I recommend this as this way you will notify the parent immediately on user click instead of waiting for the state to be set and then notifying the parent in the next render cycle (in the effect), which should be unnecessary.