-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
t
function from useTranslation
is unstable in concurrent mode
#1599
Comments
this happens only in development... because of the StrictMode... https://stackoverflow.com/questions/72489140/react-18-strict-mode-causing-component-to-render-twice try: root.render(
<App />
); |
I know that and highlighted it in the ticket title and description (StrictMode enables concurrent mode in React 18). Also, this is not acceptable solution to disable strict mode as we need it for our project. Moreover, I guess concurrent mode will be introduced and enabled always in the next React version. This is one more reason to don't remove StrictMode and cover all similar issues now. I hope you will pay a little bit more attention to discover provided Codesandbox example and fix this issue. |
Like said, also with StrictMode this happens only during development... during production it works as expected. |
Guess this is really only a result of using StrictMode which runs effects twice as obviously it works fine without using StrictMode. The The main cause (my guess setting breakpoints in useTranslation resetting What might help is changing that from just running based on the @AlexKrupko If you like you can try changing that and providing a PR if it fixes the unneeded rendering during development. |
Hi ! I will try to investigate... |
@woodreamz like already described above, this is not a direct issue (at least not in this module). It happens on purpose in React development mode (not in production). So you might try to investigate in the React code... |
@adrai I am maybe wrong but if I try the codesandbox from the bug report, using the StrictMode should render twice but it renders 3 times. For me this is the exact case where the StrictMode help to find an issue. |
it might be that is now even worse with StrictMode... (but since this is coming directly from react i have no idea) feel free to investigate |
Right, without the StrictMode, it's working well as it renders 1 time. With the StrictMode:
We should have the exact same behavior between first and second render, t should not change on second render and should not cause an extra render. It looks like the example issue given by React on their documentation where the second render made by StrictMode causes a third render. That being said, I am trying to investigate but it seems to be an issue. Could we re-open this one? |
@adrai Oh, I just discovered that using i18n.t instead of t is working well, here is the Codesandbox. This confirm the issue is in useTranslation. const { i18n } = useTranslation();
useEffect(() => {
console.log("useEffect with i18n");
}, [i18n]);
return i18n.t("text"); 2 renders with i18n, it should be the same when using the t directly. |
No response, I will open a new issue. |
feel free to create a PR to address this, if you have a solution for this |
🐛 Bug Report
t
function returned fromuseTranslation
hook is unstable and updated in a moment after mounting if use React 18 with concurrent mode. It triggers extra renders and also it trigger update of memoized values ift
is used in dependencies arrays.Please see my Codesandbox example. If you open console tab, you can see 3 calls of
useEffect
andfalse
condition whent
function changed.Expected Result
t
function should be stable even in concurrent mode.Your Environment
The text was updated successfully, but these errors were encountered: