may help you . The second parameter that you pass to useEffect tells react to skip the effect unless one of the items in the array has changed. You passed in an empty array, so other than the initial render, it will never update. Thus, you set up the event listeners with functions that have the original state in their closure, and nothing else. To get it to update as state changes, either remove the array, or fil it with variables that your code depends on:
it fixes the issue setState is async and it execute in a batch inside a scope. You are updating the state that is correct but in next line when you are doing console till that time state is not updated so it is showing the default or old value.you can use async and await. If you use class component, this.setState had one callback option which will solve this problem. But as you are using useState hook that is not available by default but there is another library for useState callback please google it. Otherwise after updating console it out side the useEffect hooks.You can get the result.
React Class vs Functional Component: When using hooks and functional components one of my functions constantly re-render
I wish this helpful for you So it turns out just wrapping things in useCallback doesn't work because I have other issues like button switching between disabled and active based on an answer being there. I decided to re-write my component to have two states one that stores overall answers and the current answer in a separate state. This way I can wrap the save answer in a useCallback with only one dependency allowing for minmal re-renders but also my buttons become active/disabled.
I wish did fix the issue. I have a Parent Class let say Data and i am Rendering Model with checkbox list if this.state.showList is true and passing this.state.list as props to functional component (see Code) , You are mutating the state here checkboxClick:
const newState = [...state]; // shallow clone of the state array, objects inside the array are not cloned
const newElement = newState[index]; // getting reference to the original object
newElement.selected = !selected; // mutating the original object
newState[index] = newElement; // replacing the original object reference with the original object reference does nothing