i have an onClick event within a child:
onClick={()=>{ //2. an image is clicked, and the choice is added to the choiceOrder state, and then a jsonupdate is called -- 3. in ChooseBy.js
//onclick, add or remove the choice
choosebyprops.setChoiceOrder(choiceOrder => {
if (choiceOrder.includes(attrprops.attrChoice)) {
const choiceIndex = choiceOrder.findIndex(attrprops.attrChoice);
choosebyprops.setJsonList((_, i) => i !== choiceIndex+1)
return choiceOrder.filter(list => list !== attrprops.attrChoice);
} else {
const newList = [...choiceOrder, attrprops.attrChoice];
return newList
}
});
attrprops.setSelectedAttr( selectedAttr=>(selectedAttr!==attr ? attr : null) );
}}
after that click, i need a list to update:
useEffect(()=>{
if(selectedAttr==null){
publishprops.setBuysideChoice( buysideChoice => ({'car_option':buysideChoice['car_option'],'zip_code':buysideChoice['zip_code']}) );
publishprops.setAttrNumber(2);
}
else {
publishprops.setBuysideChoice( buysideChoice => ({...buysideChoice,"body_type": selectedAttr}) );
publishprops.setAttrNumber(4);
};
},[selectedAttr])
useEffect(()=>{
const choiceOrder = choosebyprops.choiceOrder;
if (choiceOrder.includes(attrChoice) && choiceOrder[choiceOrder.length-1]==attrChoice) {
console.log('this json1',choosebyprops.jsonList)
choosebyprops.setJsonList(jsonList => [...jsonList, UpdateJson(allprops)]); //delayed
}
},[choosebyprops.choiceOrder])
but when the UpdateJson(allprops) function fires in the second useEffect - it's delayed. if i make a small change and save, i'll see the update in the console.log - but it doesn't fix the issue.
i've also tried adding this:
const [rerender, setRerender] = useState(false);
and adding setRerender(!rerender) in the same useEffect.
i've also tried to create a useEffect in the parent:
useEffect(()=>{
console.log(jsonList)
},[publishprops.buysideChoice])
so...i know that useEffect is an async function. what i dont understand is how to adjust that. i read some about how you need to use the previous state. i thought i had that taken care of with the call back: choosebyprops.setJsonList(jsonList => [...jsonList, UpdateJson(allprops)]);
really confused about this whole concept.