I am writing a component for my pinball game. I have to setState inside the function triggered by the 'on contact' listener. But state is not updating. Is there another design that i can try?
function Pinball(){
const[score, setScore] = useState(0);
useEffect(()=>{
//... here I set physics environment and have variable world
world.on('begin-contact', function(contact){
//...
setScore( score + 1 );
})
}, []);
return (<span>{score}</span>);
}
setState.world? did you check to see if the callback is executed onbegin-contactevent?useState. That should only be done in the functional component directly, not in a nested function. But then you can callsetScorefrom anywhere in the enclosing scope