When my checkbox gets checked or unchecked specific data needs to be set. Right now I am setting the value of checked as true or false in a state variable. The problem is it does not have a callback function like setState does so when I update it in the onChange function it does not update in time for the next set of data to get updated properly. Setting the properties on newEnhanceableData depends on if the checkbox is checked or unchecked. How do I update my checked property in time for the next line of newEnhanceableData to have it's properties updated?
Here are the state variables:
const [checkbox, setCheckbox] = useState({checked: true});
const [enhanceableData, setEnhanceableData] = useState({
name: 'Item Clicked',
properties: {
...defaultProps,
activityLocation: 'Member - RAQ Contact Details',
autopopulated: true,
changeType: 'Add'
}
});
And here are is where I update them:
<input
id='raq-sms-text-checkbox'
type='checkbox'
defaultChecked={true}
style={{marginRight: 5}}
onChange={() => {
setCheckbox({checked: !checkbox.checked});
const newEnhanceableData = enhanceableData;
newEnhanceableData.properties.changeType = checkbox.checked ? 'Add' : 'Remove';
newEnhanceableData.properties.autopopulated = false;
setEnhanceableData(newEnhanceableData)
console.log('checkbox: ', checkbox)
console.log('enhanceableData: ', enhanceableData)
sendEnhancedTrackEvent(enhanceableData);
}} />
As you can see here the checked property gets updated after enhanceableData is already set:
