I have a form, which has to be pre-populated with some value. These values come from context. I am using a reducer to handle the state of the form, so I set the initial state of the reducer from what I get from the context. Now what is happening is the context changes but the reducer state doesn't change.
const { selectedCurrency } =
useContext<CurrenciesContextType>(CurrenciesContext);
const [state, dispatch]: [CurrencyReducerState, Dispatch<any>] = useReducer(
currencyReducer as ReducerWithoutAction<CurrencyReducerState>,
selectedCurrency,
init
);
Ideally, whenever the selectedCurrency changes, it should set the initial values. But it doesn't happen. Any idea why?. I have wrapped the parent component inside the provider. Also every time the selectedCurrency changes, I see the selectedCurrency changing but the reducer state doesn't change. If the component is re-rendered then the reducer picks up the changes and initializes correctly. Is there a way to achieve this?