Reactjs: update state not show current value on next line [duplicate] - reactjs

This question already has answers here:
The useState set method is not reflecting a change immediately
(15 answers)
Closed 5 months ago.
This is my code:
const [address, setAddress] = useState("1");
const updateData = () => {
setAmount("2");
console.log(address);
}
After updateData, why printed 1? I changed it to 2.

Setting the state in React acts like an async function.
Meaning that the when you set the state and put a console.log right after it, it will likely run before the state has actually finished updating.
Which is why we have useEffect, a built-in React hook that activates a callback when one of it's dependencies have changed.
Example:
useEffect(() => {
console.log(address)
// Whatever else we want to do after the state has been updated.
}, [address])
This console.log will run only after the state has finished changing and a render has occurred.
Note: "address" in the example is interchangeable with whatever other state piece you're dealing with.
Check the documentation for more info about this.

Related

useEffect trigger explation [duplicate]

This question already has answers here:
Why useEffect running twice and how to handle it well in React?
(2 answers)
Closed 5 months ago.
Can someone explain why this even triggers the useEffect? The setState function in the useState hook always seems to be a function. Its not undefined (at initialization) at any point in time?
const [state, setState] = useState(0)
useEffect(() => {console.log('triggers')}, [setState])
I'm away of the caveats with React 18s mounting/unmounting, but since setState never changes the effect should not fire? What am I missing here?
The above code is basically the same as writing:
useEffect(() => {...}, [])
It will call on the initial render only
React will call the callback function of useEffect on the initial render and when any of the dependency changes from dependency array.
useEffect(() => {
console.log("triggers");
}, [setState]);
CODESANDBOX DEMO
So, The callback function will trigger on the inital render and when the setState changes (which will never change)
Long story short, useEffect will always run when the component first loads, even if you have a dependency list,
useEffect(()=> { console.log("hello") }) // will log "hello"
useEffect(()=> { console.log("hello") } []) // will log "hello"
useEffect(()=> { console.log("hello") } [x]) // will log "hello"
useEffect(()=> { console.log("hello") } [x, y, z]) // will log "hello"
if you only want your useEffect to fire if the dependency element in the dependency list changes, you need to set some logic inside, for instance, for your case:
const [state, setState] = useState(0)
useEffect(() => {
if(!state) return
console.log('triggers')
}, [state])
and the same if you have something else to check, for instence if you have the initial state set to an empty array, you would do the condition like if(!state.length) return, and so on.
This is kinda sucks when you have multiple elements in the dependency list, that's why the react developers recommend having mutliple useEffects in your component for such case, where each useEffect() handles a piece of logic.

React setState update is happening late ( n-1) , it seems i am lagging by one [duplicate]

This question already has answers here:
The useState set method is not reflecting a change immediately
(15 answers)
Closed 2 years ago.
const handlePageChange = (page, itemCount) => {
setActivePage(page); // This is how am updating my state variable
setItemsPerPage(itemCount); // This is how am updating my state variable
}
handlePageChange is called from onclick() , but it seems like activepage update is happening late ( it seems i am lagging by one) , if I go to 2nd page my state variable of active page will show 1
Since setActivePage is the asynchronous method, you can't get the updated value immediately after setActivePage. You should use another useEffect with dependency to see the updated activePage.
const handlePageChange = (page, itemCount) => {
setActivePage(page);
console.log(activePage); // Old `activePage` value will be printed.
...
}
useEffect(() => {
console.log(activePage) // print state variable inside useEffect.
}, [activePage]);

React Hooks state always one step behind [duplicate]

This question already has answers here:
The useState set method is not reflecting a change immediately
(15 answers)
Closed 2 years ago.
I have various functions in React where I need to change the state with useState then do some action conditional on whether the new state meets some criteria.
This uses a setValues method in useState to set the value of newpassword when handleChange is called with prop="newpassword". The new password is then evaluated with a regex test, and if it is valid the state variable passwordIsValid should be set to true.
const handleChange = prop => event => {
setValues({ ...values, [prop]: event.target.value })
if (prop === 'newpassword' && passwordValidation.test(values.newpassword)) {
setValues({ ...values, passwordisValid: true })
console.log(prop, values.passwordisValid)
} else {
console.log(prop, values.passwordisValid)
}
}
The state is always one step behind tho - and I know this is because useState is async, but I don't know how to use useEffect to check the state? Very new to hooks, could someone help me out?
useState() hook is just a function call. It returns value and function pair. values is just a constant it doesn't have any property binding.
// Think of this line
const [values, setValues] = useState(0);
// As these two lines
const values = 0;
const setValues = someFunction;
When you call setValues react updates value for the next render. Next time component renders, it will call useState again which will return new values.
As a solution you should use event.target.value. You don't want to use it directly though because event.target is nullified after you observe it.
const newValue = event.target.value
// use newValue to setValues etc
Inside any particular render, props and state forever stay the same and Every function inside the component render (including event handlers, effects, timeouts or API calls inside them) captures the props and state of the render call that defined it. For that reason if you try to access values.newPassword in your event handler you will always get the state for that particular render i.e the old password.
Just think of useState as a function that returns the state for that particular render and that state is immutable for that particular render.

Stop hooks re-fetching data on component update [duplicate]

This question already has answers here:
Infinite loop in useEffect
(16 answers)
Closed 3 years ago.
I am new to react hooks and as i read in docs useEffect runs on every update of component and on first render also (so it is componentDidMount and componentDidUpdate combined) but when i am fetching data inside useEffect it will re-do it when state updates and so if I set state value after fetching data from server it will create infinite loop. and i want to prevent it and only fetch it for first render like in componentDidMount, how can i implement this?
useEffect() takes a dependencies as an array parameter. This dependency(ies) acts as a decider such that, if one of the dependencies' value change, useEffect is invoked.
It's effectively equivalent to saying:
shouldComponentUpdate(nextProps, nextState) {
// If props or state changes, update the component
if(this.props.someProp != nextProps.someProp || this.state.someState != nextState.someState){
return true;
}
return false;
}
With useEffect() :
useEffect(() => {
//Do stuff here
}, [somePropValue, someStateValue]);
But to say that only do once, specify an empty dependency array(ideal for API calls that happens once on load):
useEffect(() => {
//Do stuff here
}, []);

How does react hooks handles callback when it sets the state? [duplicate]

This question already has answers here:
How To Access Callback Like in setState from useState Hook
(2 answers)
Closed 3 years ago.
In a class-based component when one would set the state, it would take the argument of the new state and sometimes one could fire another function within it. In a functional-based component, using hooks, how can I accomplish the same goal?
// class-based componenet example:
state = {
count: 0
}
this.setState({
count: count +1
}, () => someFunction())
// functional-based componenet example:(how to fire someFunction() when the state is set like in the class-based componenet?)
const [count, setCount] = useState(0)
setCount(count +1)
I'm aware that hooks don't take a second argument but can something similar be done?
I think the useEffect hook would help you here. It basically acts as a componentDidUpdate:
useEffect(() => {
doSomething();
}, [count]);
The second parameter means that the effect / function will only fire if count changes.

Resources