I have a component that creates three radio buttons. Cicking one should update a context store I have elsewhere.
My state looks like this:
const styles = {
font: {
size: {
value: '22',
unit: 'px'
},
weight: 'bold',
color: '#663300',
family: 'arial',
align: 'center'
}
};
I store my state like this:
const myContext = useEmailContext();
const { ...styling } = styles;
const [style, setStyle] = useState({ styling });
And then my component fires the functions onChange
:
return (
<RadioButtonGroup
onChange={(event) => {
setIsChecked({ checked: event.target.value });
setStyle({ ...styling, font: { ...styling.font, align: event.target.value } });
console.log(style);
myContext.setStyles(style);
}}
/>
When I click a button the function fires, but the console.log shows the previous state, not the newly updated one. Similarly, my context also get updated one step behind.
What's going on here?