This will have something to so with async but I can't get to the bottom of it.
Problem: when I log my global state object to console, it is not returning all the properties. Despite the fact that the missing property can be logged to console independently.
When I log state.workflow
(the overall object that stores the global state) I get:
{
"workflow_id": "", // Never updates
"workflow": [...] // Always updates
}
But if I separately log (state.workflow_id
) it gives me the value.
My reducer:
export default function workflowReducer(state, action) {
switch (action.type) {
case 'SET_WORKFLOW_ID':
return action.payload;
case 'SET_WORKFLOW_NAME':
return action.payload;
case 'ASSIGN_WORKFLOW':
return action.payload;
case 'ADD_NODE':
return {workflow: [...state.workflow, action.payload]};
default:
return state;
}
}
My context (the dispatches run on init):
export const WorkflowProvider = ({children}) => {
const [state, dispatch] = useReducer(workflowReducer, initialState);
const [initialized, setInitialized] = useState(false);
useEffect(() => {
if (initialized) return;
getStoredCanvas().then((response) => {
setInitialized(true);
dispatch({
type: 'SET_WORKFLOW_ID',
payload: {
...initialState,
workflow_id: response.data.workflow_id
? response.data.workflow_id
: JSON.stringify(mintGuid()),
},
});
dispatch({
type: 'ASSIGN_WORKFLOW',
payload: {
...initialState,
workflow: response?.data.elements,
},
});
});
}, [dispatch, initialized]);
return (
<React.Fragment>
{initialized && (
<WorkflowContext.Provider value={{state, dispatch}}>{children}</WorkflowContext.Provider>
)}
</React.Fragment>
);
};
My state:
const initialState = {
workflow_id: '',
workflow_name: undefined,
workflow: [],
};
My logging method:
const {state} = useContext(WorkflowContext);
const sendRequest = useCallback( () => {
if (isSending) return;
setIsSending(true);
console.group('%c Data to [Workflow] EP ', 'background: #222; color: #bada55');
console.log(state);
console.groupEnd();
if (isMounted.current) setIsSending(false);
}, [isSending, state]);
Thank you for any help.