import React,{useEffect,useState} from 'react';
export function App() {
const [val,setVal] = useState(1) ;
useEffect(()=>{
console.log("before changing",val) ;
setVal(val+1) ;
console.log("after changing",val) ;
},[]) ;
return (
<div className='App'>
</div>
);
}[tag:tag-name]
I am trying to update value of ‘val’ using setVal function inside a useEffect , but its seem to didn’t work
New contributor
Indrajit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Changes to state are scheduled, so you will not see it until the next render.
If you showed the value somewhere in the DOM you would see it works.
export function App() {
const [val,setVal] = useState(1) ;
useEffect(()=>{
console.log("before changing",val) ;
setVal(val+1) ;
console.log("after changing",val) ;
},[]) ;
return (
<div className='App'>
{val}
</div>
);
}