question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditionally rendering FocusTrap causes immediate deactivation with Strict Mode

See original GitHub issue

This 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.

Here’s a barebones CodeSandbox demo of the problem.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
scatterfishcommented, Sep 8, 2022

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.

1reaction
scatterfishcommented, Sep 9, 2022

Hmm, I don’t think we’re talking about the same thing. I linked a fork of your sandbox where if (props.isOpen) is now if (props.isOpen || true) and it still works in strict mode. Note that props.isOpen remains false until the button is clicked, though, which means the trap remains inactive because it’s controlled by active={props.isOpen}, hence it doesn’t activate, and doesn’t get re-activated after the unmount/remount caused by strict mode, and so the handler isn’t called.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found