-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Use useIsFocused
may lead to app crash
#12012
Comments
Hey @badeggg! Thanks for opening the issue. It seems that the issue doesn't contain a link to a repro. The best way to get attention to your issue is to provide an easy way for a developer to reproduce the issue. You can provide a repro using any of the following:
A snack link is preferred since it's the easiest way to both create and share a repro. If it's not possible to create a repro using a snack, link to a GitHub repo under your username is a good alternative. Don't link to a branch or specific file etc. as it won't be detected. Try to keep the repro as small as possible by narrowing down the minimal amount of code needed to reproduce the issue. Don't link to your entire project or a project containing code unrelated to the issue. See "How to create a Minimal, Reproducible Example" for more information. You can edit your original issue to include a link to the repro, or leave it as a comment. The issue will be closed automatically after a while if you don't provide a repro. |
The versions mentioned in the issue for the following packages differ from the latest versions on npm:
Can you verify that the issue still exists after upgrading to the latest versions of these packages? |
Think about change react-navigation/packages/core/src/useIsFocused.tsx Lines 16 to 23 in 52a3234
React.useEffect(() => {
if (isFocused !== valueToReturn) {
// If the value has changed since the last render, we need to update it.
// This could happen if we missed an update from the event listeners during re-render.
// React will process this update immediately, so the old subscription value won't be committed.
// It is still nice to avoid returning a mismatched value though, so let's override the return value.
// This is the same logic as in https://github.com/facebook/react/tree/master/packages/use-subscription
setIsFocused(valueToReturn);
}
}, [isFocused, valueToReturn]); There must be a re-render leads to React.useEffect(() => {
if (isFocused !== valueToReturn) {
// If the value has changed since the last render, we need to update it.
// This could happen if we missed an update from the event listeners during re-render.
// React will process this update immediately, so the old subscription value won't be committed.
// It is still nice to avoid returning a mismatched value though, so let's override the return value.
// This is the same logic as in https://github.com/facebook/react/tree/master/packages/use-subscription
setIsFocused(valueToReturn);
}
}, [isFocused, valueToReturn]); be checked. If condition So after the change, logic will be the same, except that set an identical value with previous value ( |
Think about simply delete react-navigation/packages/core/src/useIsFocused.tsx Lines 16 to 23 in 52a3234
This code block is for reason that
The only effect of So maybe we should simple delete this code block, if I am not missing something. |
Codesandbox link doesn't work. |
@satya164 Hi, please try codesandbox link again. I forgot to change 'who can access'. |
The codesandbox doesn't show an issue with the logic in In the codesandbox, you're updating the state if Also please provide a repro using where using the hook from React Navigation crashes your code, not an approximation of the logic we have - which may not match what's actually happening. |
I'll try again to provide an exact minimal repro. The code snippet in codesandbox is trying to say, we should not call Anyway, the loop is like this:
|
It needs to provide a valid reason to say that. The pattern is also used in React docs https://react.dev/reference/react/useState#storing-information-from-previous-renders
If it doesn't satisfy the condition and does a state update, then the next render with the new state will satisfy the condition. If it doesn't then there's a different problem.
It'll only loop if the |
@satya164 Hi, do you know in what situation, condition I noticed the comment line, but can not find a way to trigger.
|
effects run asynchronously. it's possible that there was a state change/navigation before the effect could run. however, it's an edge case and the logic handles that. i don't know how to trigger this edge case manually. but according to your issue, it becomes |
Yes, I do notice the Expensify App is experiencing that condition met, e.g. this issue and this issue. The later one only happen on physical iOS. Just, 'expensify app' is a big app, it's kind difficult to find out what is triggering. In the two issues I just mentioned, if I wrap if (isFocused !== valueToReturn) {
// If the value has changed since the last render, we need to update it.
// This could happen if we missed an update from the event listeners during re-render.
// React will process this update immediately, so the old subscription value won't be committed.
// It is still nice to avoid returning a mismatched value though, so let's override the return value.
// This is the same logic as in https://github.com/facebook/react/tree/master/packages/use-subscription
setIsFocused(valueToReturn);
} into That's why I reported this issue. It's rational I suspect this code block. Still trying to make a minimal repro... |
Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it. |
Current behavior
Use
useIsFocused
may lead to app crashThe causing code lines are
react-navigation/packages/core/src/useIsFocused.tsx
Lines 16 to 23 in 52a3234
I must say that if we write code lines like:
inside a react function component, the function component will loop forever. This is because:
Please refer react doc here or this stack overflow answer. So code
react-navigation/packages/core/src/useIsFocused.tsx
Lines 16 to 23 in 52a3234
Check this code sandbox demo to verify what I'm saying.
A loop case could be: Expensify/App#42824 (comment), or Expensify/App#42916
It's kind difficult to make a minimal exact reproduction(I tried literally one day), but I think what I am explaining above is convincible enough.
Expected behavior
use
useIsFocused
must not lead to app crashReproduction
Loop cases Expensify/App#42824 and Expensify/App#42916, will be fixed by applied those changes
Code sandbox demo to verify the bad code style: https://codesandbox.io/p/sandbox/young-platform-phwfhv?file=%2Fsrc%2FApp.js%3A22%2C29
To remove label "needs repro" https://github.com/badeggg/expensify-app
Platform
Packages
Environment
The text was updated successfully, but these errors were encountered: