Skip to content

Conversation

@Malien
Copy link
Contributor

@Malien Malien commented Apr 7, 2025

I was trying to accomplish an exit animation for routes rendered in <Outlet />s. I need to hold onto the previous value of outlet until it finishes its animation.

const outlet = useOutlet()
const [heldOntoNode, setHeldOntoNode] = useState(outlet)

// If outlet changes to null, don't discard `heldOntoNode`.
// Otherwise, heldOntoNode should reflect the latest value of the outlet
if (outlet && heldOntoNode !== outlet) {
  // Calling setState in render body will discard the current render
  // and schedule a new one immediately
  setHeldOntoNode(outlet)
  return null
}

if (!heldOntoNode) return null

return (
  <Modal 
    // outlet set to null means we have to hide the modal
    // aka. trigger it's exit animation.
    open={!!outlet} 
    onExitAnimationEnd={() => setHeldOntoNode(null)}
  >
    {heldOntoNode}
  </Modal>
)

Calling setState in render is THE way to capture the previous value. Reading refs in render isn't really allowed.

Since useOutlet wraps the <RenderedRoute /> in <OutletContext.Provider />, the reference to the outer <OutletContext.Provider /> is always different on every re-render. And the code above triggers an infinite render loop.

This PR just wraps the resulting element in useMemo. That's it.

The workaround for me currently involves asserting that the element is in fact the react context, and grabbing the stable children prop.

I'm not sure we can guarantee stable references from useOutlet going forward, but that would be nice.

@changeset-bot
Copy link

changeset-bot bot commented Apr 7, 2025

🦋 Changeset detected

Latest commit: dab6471

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
react-router Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/dev Patch
react-router-dom Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Apr 7, 2025

Hi @Malien,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@Malien Malien changed the title Make element returned by useOutlet have stable reference Make element returned by useOutlet have stable-ish reference Apr 7, 2025
@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Apr 7, 2025

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@brophdawg11
Copy link
Contributor

Could you add a changeset for this?

@Malien
Copy link
Contributor Author

Malien commented Sep 14, 2025

@brophdawg11 Yeah. I did. Sorry for the late change

@brophdawg11 brophdawg11 self-requested a review September 16, 2025 21:00
@brophdawg11 brophdawg11 merged commit e04cb92 into remix-run:dev Sep 17, 2025
5 checks passed
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 7.9.2-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 7.9.2 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants