I have a react application with lots of components. I am using React context api to manage the state in the application.
My react state in context looks like below.
{ key1: value1, key2: value2, key3 : { key4 : { key5 : value5 } } }
So, it's basically a nested json.
Now, one of the keys let's say keys 3 is being updated from many of the components in react component tree. It could be updated by any component in the tree hierarchy.
So, here in this case, I want to call backend api whenever key3 changes. How do I do this in react?
EDIT 1:-
Below is how I have setup my context.
import React, { createContext, useEffect, useReducer } from "react";
import { reducer, initialState } from "./reducers";
import { useActions } from "./actions";
const AppContext = createContext(initialState);
const AppProvider = ({ children }: { children: any }): any => {
const [state, dispatch] = useReducer(reducer, initialState);
const actions = useActions(state, dispatch);
useEffect(() => {
console.log("Called ");
}, [state.decision])
return (
<AppContext.Provider value={{ state, dispatch, actions }}>
{children}
</AppContext.Provider>
);
};
export { AppContext, AppProvider };