Artem Krasovskyi’s Post

I'm going to talk about two things in it: Activity and useEffectEvent. Both were experimental for two-plus years. Both are now stable. Activity You know the drill with conditional rendering. Works fine until users complain that switching tabs clears their form. The component unmounts, state goes with it. Activity mode="hidden" keeps the component alive but out of sight. Effects get unmounted, so nothing runs in the background. Updates pause until React clears its queue. State stays. The mental model that clicked for me: you're parking the component, not deleting it. Tabbed UIs, back navigation that remembers scroll and input state, pre-loading a page while the user is still on another one — these all make sense now without reaching for manual state hoisting. useEffectEvent This one fixed something that's been annoying me for a long time. Say you have an effect that opens a WebSocket. When the connection is established, you show a notification. The notification reads from a theme prop. If you add theme to the dependency array, the socket reconnects every time the theme changes. If you leave it out, the linter is unhappy. useEffectEvent lets you pull the notification logic into a separate function. That function always reads the latest theme but doesn't count as a reactive dependency. The socket reconnects when the room changes. Not when the theme does. A few rules to know: don't put useEffectEvent functions in dependency arrays, don't call them during render, don't pass them to child components. They're not a general escape hatch. They're for logic that behaves like an event — something that fires from inside an effect but shouldn't control when that effect runs. What actually changed For me, both of these reduce the number of cases where the right answer was "hoist the state up" or "add an eslint-disable comment and move on." That's not nothing. #react #frontend #javascript #webdev #reactjs #frontenddevelopment #softwaredevelopment

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories