export const LocaleProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, { locale: DEFAULT_LOCALE });
useEffect(() => {
const storedLocale = getStoredLocale();
if (storedLocale) dispatch(changeLocale(storedLocale));
}, []);
useEffect(() => {
const { locale: currentLocale } = state;
saveLocale(currentLocale);
}, [state, state.locale]);
return (
<LocaleContext.Provider value={[state, dispatch]}>
{children}
</LocaleContext.Provider>
);
};
How to watch only a single field in an object, state. As you can see in the second effect, when I watch only [state.locale] my VS code shows an eslint warning(react-hook/exhaustive-deps), React Hook useEffect has a missing dependency: 'state'. Either include it or remove the dependency array. When I save my code VS code adds state in the dependencies array ([state, state.locale]).