I have an application using hooks using state
and setState
as variable names, hope that is not too confusing. I try to call setState(newState)
and then run my rotate()
function afterward when I check rotate it still has my slots
empty. As if it did not update in time. Do I need to run this as a callback? Is setState
not finished by the time setState
is finished?
const [ state, setState ] = useState({ manual: true, rings: [], slots: [] });
const slotMap = {
0: 288,
1: 216,
2: 144,
3: 72,
4: 360
};
useEffect(()=>{
const newState = { ...state, slots: props.slots };
console.log('DEGREES:');
newState.slots.forEach(x => console.log(slotMap[x])); //works!
if(newState.slots !== undefined && props.apiHit === true ) {
setState(newState);
rotate();
}
}, [props.slots, props.apiHit]);
rotate =() => {
console.log(state);
}
output:
{manual: true, rings: Array(3), slots: Array(0)}manual: truerings: (3) [{…}, {…}, {…}]slots: Array(0)length: 0__proto__: Array(0)__proto__: Object
I definitely have props.slots
having data. It just doesn't seem to populate when rotate is called. What am I missing in understanding about setState
?