i have a custom hook that is being used in multiple components...the problem is when updating the hook value from one component it updates the component state but the other components do not update or remount
here is the first component:
import useTranslation from "./customHook";
export default component1 = () => {
const { t } = useTranslation();
return <div>{t()}</div>;
};
here is my second component that updates the hook on button click:
import useTranslation from "./customHook";
export default component2 = () => {
const { t, setLanguage } = useTranslation();
return (
<button
onClick={() => {
setLanguage(t() === "ar" ? "en" : "ar");
}}
>
{t()}
</button>
);
};
and here is the hook itself
import { useCallback, useState } from "react";
export default function useTranslation() {
const [language, setLanguage] = useState("ar");
const [fallbackLanguage, setFallbackLanguage] = useState("en");
const translate = useCallback(() => {
return language;
}, [language]);
return {
language,
setLanguage,
fallbackLanguage,
setFallbackLanguage,
t: translate
};
}
what I want to achieve is that component2 gets updated or remounted codesandbox :https://codesandbox.io/s/goofy-fast-q39hx?file=/src/customHook.js:0-403