when I do console.log in home.js, it shows me the previous second state of the selectSprint. can you explain me what is the problem with the useState hook ?
I have two components in different files, one is home.js and another is iteration.js. In home.js, the code is like written below:
`
---------
export default function DataTable(){
const [selectSprint, setSelectSprint] =useState("none")
useEffect(()=>{
console.log(selectSprint)})
return (
<>
<Interation setSelectSprint = {setSelectSprint}/>
</>
)
}
in Iteration.js
`
export default function Iteration(props){
const handleChange= (event)={
props.setSelectSprint(sprint[value-1]
}
return (
<Select value={value} onChange={handleChange}>
<MenuItem value={0}>
2023
</MenuItem>
<MenuItem value={1}>
2024
</MenuItem>
<MenuItem value={2}>
2025
</MenuItem>
<MenuItem value={3}>
2026
</MenuItem>
</Select>
)
}`
Example like: we just take a case. means I have clicked
2024 it shows undefined.
for 2025, it shows 2023.
for 2023, it shows 2024.
for 2025, it shows 2025.
it takes the previous second value.
`
useEffect(() => {
console.log("Current state:", selectSprint);
}, [selectSprint]);`
Have tried this, but problem is same.
2
Answers
Please change the
sprint[value-1]
tosprint[value]
that is causing the previous value to be taken.stackblitz
and