Conditionally rendering FocusTrap causes immediate deactivation with Strict Mode
See original GitHub issueThis doesn’t appear to be quite the same issue as #720 or #738. Using v10.0.0
.
I have a modal component that only renders itself when active, and as part of this modal component I use FocusTrap
. When in Strict Mode, opening the modal causes onDeactivate
and/or onPostDeactivate
to get called immediately.
The focus trap works perfectly fine either without Strict Mode or without being conditionally rendered.
Issue Analytics
- State:
- Created a year ago
- Comments:22 (12 by maintainers)
Top Results From Across the Web
React conditional rendering: 9 methods with examples
In React, conditional rendering refers to the process of delivering elements and components based on certain conditions.
Read more >Only conditionally re-render when context value updates
memo to achieve re-rendering only on value changes for that component. I have done something like here in this sandbox. index.js import { ......
Read more >Conditional Rendering - React Deep Dive: From Beginner ...
A React component renders a state of a user interface based on its props and its current state. In an ideal situation, this...
Read more >How to stop useEffect from running twice on mount ... - YouTube
Learn how to stop useEffect from running twice on mount or first render in React. The new React 18 strict mode runs useEffect...
Read more >A React component that traps focus
Conditionally rendering FocusTrap causes immediate deactivation with Strict Mode. This doesn't appear to be quite the same issue as #720 or #738 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m honestly convinced now that how it currently behaves is the correct behavior, and that relying on using
onDeactivate
for side-effecting code is the user’s problem. All the documentation really needs is a large bold warning about it, maybe some suggestions for how to deal with it, and definitely the rationale behind it so that it doesn’t take people by surprise.If my guess about the unexpected behavior I mentioned above is in fact due to the Strict Mode-specific mitigations already in place, then truthfully trying to muck with things more than they already have will do more harm than good.
Ah, that’s my mistake, I had forgotten about that. Makes sense. To be clear, I wasn’t saying before that the modal didn’t work, just that the
onDeactivate
wasn’t being called when the app loads, which is now explained by this.