Most active commenters
  • adithyassekhar(4)
  • jitl(4)
  • chamomeal(3)

←back to thread

205 points samspenc | 27 comments | | HN request time: 1.072s | source | bottom
1. adithyassekhar ◴[] No.45146246[source]
This reminded me, I saw tooltips being a large chunk when I profiled my react app. I should go and check that.

Similarly, adding a modal like this

{isOpen && <Modal isOpen={isOpen} onClose={onClose} />}

instead of

<Modal isOpen={isOpen} onClose={onClose} />

Seems to make the app smoother the more models we had. Rendering the UI (not downloading the code, this is still part of the bundle) only when you need it seems to be a low hanging fruit for optimizing performance.

replies(4): >>45146382 #>>45146410 #>>45147091 #>>45147510 #
2. pathartl ◴[] No.45146382[source]
In the Blazor space we use factories/managers to spawn new instances of a modal/tooltip instead of having something idle waiting for activation.

The tradeoff is for more complicated components, first renders can be slower.

3. trylist ◴[] No.45146410[source]
I remember solving this problem before. These are both global components, so you create a single global instance and control them with a global context or function.

You basically have a global part of the component and a local part. The global part is what actually gets rendered when necessary and manages current state, the local part defines what content will be rendered inside the global part for a particular trigger and interacts with the global part when a trigger condition happens (eg hover timeout for a tooltip).

replies(1): >>45148454 #
4. aiiizzz ◴[] No.45147091[source]
That breaks the out transition.
replies(8): >>45147226 #>>45147342 #>>45147669 #>>45148400 #>>45148647 #>>45150589 #>>45150865 #>>45153296 #
5. amatecha ◴[] No.45147226[source]
So, win-win? I want a modal to get out of the way as fast as possible, any fade/transition animations are keeping me from what I want to look at. :)
replies(1): >>45151902 #
6. akie ◴[] No.45147342[source]
Unless you set `isOpen` only when the transition has ended
replies(1): >>45147405 #
7. debugnik ◴[] No.45147405{3}[source]
Isn't isOpen = false what triggers the transition in the first place here?
8. Cthulhu_ ◴[] No.45147510[source]
Alternatively, how many modals can be open at any given time? And is it a floating element? May be an option to make it a global single instance thing then, set the content when needed. Allows for in/out transitions, too, as another commenter pointed out. See also "Portals" in React.
replies(1): >>45150676 #
9. abdusco ◴[] No.45147669[source]
Even when using view transitions?

https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-s...

replies(1): >>45147893 #
10. csande17 ◴[] No.45147893{3}[source]
The "Transitioning elements on DOM addition and removal" example in that article uses a setTimeout() to wait an extra 1000 milliseconds before removing the element from the DOM. If you immediately remove the element from the DOM (like would usually happen if you do {isOpen && <Modal />} in React), it'll vanish immediately and won't have time to play the transition.
11. btown ◴[] No.45148400[source]
You can create a ref that stores whether isOpen has ever been true, and condition on that, letting you lazily initialize the Modal and its contents while preserving out transitions. I’m honestly surprised this isn’t recommended a lot more often!
12. high_priest ◴[] No.45148454[source]
React devs re-discovering DOM manipulation... SMH.

This is, in general, the idea that is being solved by native interaction with the DOM. It stores the graphic, so it doesn't have to be re-instated every time. Gets hidden with "display:none" or something. When it needs to display something, just the content gets swapped and the object gets 'unhidden'.

Good luck.

replies(2): >>45149735 #>>45149852 #
13. zarzavat ◴[] No.45148647[source]
Good. Transitions are meant to serve a purpose, showing what came from where. A modal doesn't need a transition, it should just disappear instantly. Like closing a window. The user is not helped by animating that something disappears when they close it, they already knew that.
14. jitl ◴[] No.45149735{3}[source]
The post you’re replying to is saying they went FROM always having the component mounted (at least in the component tree if not in the DOM as display:hidden), TO only mounting the component when it needs to be open. They moved from the way you’re talking about, to creating the component/DOM nodes only when needed.

Excessive nodes - hidden or not - cost memory. On midrange Android it’s scarce and even if you’re not pushing against overall device memory limit, the system is more likely to kill your tab in the background if you’ve got a lot going on.

replies(2): >>45150665 #>>45151065 #
15. ◴[] No.45149852{3}[source]
16. adithyassekhar ◴[] No.45150589[source]
Good point, luckily there are no transitions for modals in this project.
17. adithyassekhar ◴[] No.45150665{4}[source]
Especially when you know the user won't be opening half of those. I did'nt use a global one because the modals themselves have some complex logic inside.
replies(1): >>45152895 #
18. adithyassekhar ◴[] No.45150676[source]
There is only one. Won't contexts cause rerenders through the tree? We already use portals. It's just each modal have complex logic inside them that they're their own component.
19. jitl ◴[] No.45150865[source]
You should rethink your modal system if removing the controller component from the render tree doesn’t transition out
replies(1): >>45151921 #
20. llbbdd ◴[] No.45151065{4}[source]
"Devs use React because they how to use the web platform, here's how to do it right" and then posts a vanilla solution that doesn't solve understand or solve the problem. Tale as old as time. Bonus points if covering the edge cases in the vanilla solution and making it work for a second component would involve a tiny homegrown reimplentation of most of React anyway.
21. chamomeal ◴[] No.45151902{3}[source]
The designers don’t want to break the out transition, and the PM wants whatever the designer wants
22. chamomeal ◴[] No.45151921{3}[source]
I mean in react if it’s gone from the render tree, it’s gone. I know there are libs like transition-group but I honestly don’t understand how they work.

And the “react way” is to have the UI reflect state. If the state says the modal is not being rendered, it should not be rendered

replies(1): >>45152523 #
23. jitl ◴[] No.45152523{4}[source]
Yes, React’s main idea is f(state) -> UI; but what’s returned from render is a declarative specification of what the UI should be. It’s up to React (and library authors) to make sure the UI ends up as we specify without our app logic needing to be concerned with how that happens. I view managing transition out animations for a component removed from the render tree the same way: I’m happy if the incidental complexity is encapsulated in a library (either a third party one or something I write myself), rather than spread across the whole app.

There are many high quality third party tools to help with this, such as Motion’s <AnimatePresence> (https://motion.dev/docs/react-animate-presence). I haven’t used the library you mentioned, but it seems somewhat unmaintained and isn’t compatible with react-dom@19.

First party support is coming to React with the new <ViewTransition> component (https://react.dev/reference/react/ViewTransition).

If you insist that only the React maintainers are allowed to diverge DOM state from the render tree or write code you don’t understand, you can adopt it today from react{,-dom}@experimental. It’s been available there since April (https://react.dev/blog/2025/04/23/react-labs-view-transition...).

replies(1): >>45164508 #
24. ◴[] No.45152895{5}[source]
25. socalgal2 ◴[] No.45153296[source]
You can add some TransitionManager that uses a bool prop whether or not to render its children and when the prop goes from true to false, keeps rendering its children for some amount of time.
26. jitl ◴[] No.45164508{5}[source]
For anyone curious, here's a proof-of-concept of how to implement a <Modal> that manages its own transition out when it's unmounted using just ReactDOM.createPortal and React.useLayoutEffect.

https://stackblitz.com/edit/react-modal-with-transition-out?...

replies(1): >>45169343 #
27. chamomeal ◴[] No.45169343{6}[source]
awesome thanks!!

Ok it all makes sense now. I’d forgotten about useLayoutEffect. Haven’t had to write much react recently.