Keeping Screens Visible With <Activity mode="hidden">

One of my plans for React Navigation 8 is to use <React.Activity> to unmount effects on inactive screens by setting mode="hidden". It will improve performance and memory usage by removing subscriptions and updates on screens that aren’t active.

It is essentially what react-native-screens does today with its experimental react-freeze integration, but better integrated with React Navigation as we have more control over when the screens are frozen.

However, <Activity mode="hidden"> hides the content, which is not ideal for our use case. Especially on a stack navigator where you can swipe back to the previous screen with gestures. If we don’t manage to make the previous screen visible in time, the user will see a flash of blank screen when they start swiping back. This is especially tricky in React Native as the native event isn't delivered synchronously, and we don’t have high-priority state updates like ReactDOM.flushSync to toggle visibility immediately.

So I experimented with making screens with <Activity mode="hidden"> stay visible, while still unmounting effects. I’ll share the implementation details here in case it can be useful for others.

What <Activity mode="hidden"> does

When you set mode="hidden" on an <Activity>, it does two things:

  • Unmounts effects for all React components inside the activity
  • Hides the content from the screen with display: none

This means cleanup functions in useEffect or useLayoutEffect will run, so subscriptions get removed, timers get cleared, etc. which avoids unnecessary updates for hidden content.

In other words, the content is still there, but it’s (almost) frozen and invisible.

Going rogue: Keeping content visible

What we want is the frozen part, but not the invisible part. We could do that by either preventing display: none from being applied, or by undoing it immediately after it’s applied. Both approaches were necessary depending on the platform.

Native platforms

On native platforms like Android and iOS, we use RCTView (the name of the native view class for View) and pass a custom configuration to it.

This custom view configuration supports the following properties relevant to our use case:

  • uiViewClassName: the name of the native view class to use. We set it to RCTView so it behaves like a normal View.
  • validAttributes: a map of attributes supported by the view. It’s a record of attribute names to either true (if the attribute is passed as-is) or an object with a process function that transforms the value before it’s applied to the native view.

The component already supports all attributes of a normal View by default. What we pass in our view configuration is a partial override which will be merged with the default configuration.

In our case, we need to override the style attribute, so we can override how display style is handled with the process function. We always return contents for any value, even for none. This way, when Activity sets display: none, it will be overridden to display: contents and the content will stay visible.

The implementation looks like this:

import { NativeComponentRegistry } from "react-native";

const viewConfig = {
  uiViewClassName: "RCTView",
  validAttributes: {
    style: {
      display: {
        process: (value) => "contents",
      },
    },
  },
};

export const MyCustomView: HostComponent = NativeComponentRegistry.get(
  "MyCustomView",
  () => viewConfig,
);

In this case, MyCustomView is a view that only supports the display style, and it will always override it to contents. We use contents so that the view itself doesn’t affect layout and the content is directly visible.

Then we can use this view inside the Activity:

<Activity mode={active ? "visible" : "hidden"}>
  <MyCustomView style={{ display: "contents" }}>
    {children}
  </MyCustomView>
</Activity>

Now mode="hidden" can unmount effects for inactive content, but when display: none is applied, it will be overridden to display: contents and the content will stay visible.

I got the idea for this approach from react-native-screens which got it from Navigation router.

It overrides the validAttributes in a ref callback by accessing the internal __viewConfig property of the view instance, which is a bit hacky and applies to all instances of the view, not the only one where it’s called, which may be a bit unexpected.

Web

On the web, we can’t define our own div which overrides how display is applied. So we undo the display: none style immediately instead.

To do this, first, we get the node by using a ref callback, and then use MutationObserver to track changes to the style attribute.

The one 'gotcha' here is that ref for the div under Activity will be cleaned up when mode="hidden" is set, so we add another wrapper div around Activity and attach the ref to it, then observe its children for style changes.

The implementation looks like this:

const onRef = useCallback((node) => {
  const observers = node.childNodes.forEach((child) => {
    const observer = new MutationObserver(() => {
      child.style.display = "contents";
    });

    observer.observe(child, {
      attributes: true,
      attributeFilter: ["style"],
    });

    return observer;
  });

  return () => {
    observers.forEach((observer) => observer.disconnect());
  };
}, []);

return (
  <div ref={onRef} style={{ display: "contents" }}>
    <Activity mode={active ? "visible" : "hidden"}>
      <div style={{ display: "contents" }}>{children}</div>
    </Activity>
  </div>
);

In the future, it will be possible to use a <Fragment> instead of the wrapper div and pass the ref to it. At the time of writing, it’s only supported in React’s canary versions, so I didn’t use it.

This approach is inspired by the article “A React trick to improve exit animations”. It suspends the component instead of using <Activity> (same as what react-freeze does), but the way it keeps the content visible is the same.

Conclusion

Overriding the display style this way feels a bit hacky, and needs more testing to ensure it works correctly across all scenarios, but it greatly improves the user experience with stack-based navigation that offers animations and gestures. I hope React will provide a built-in way to keep content visible in the future, but for now, this is a decent workaround that works across platforms.

Insights

Learn more about React Native

cover

Aug 4Podcast

VisionCamera V5 With Marc Rousavy

Marc Rousavy joins Mike Grabowski to unpack VisionCamera v5, Nitro Modules, the constraints API, migration work, and what changed for React Native camera apps.

cover

Jul 1Podcast

Redraw for Effects Beyond Skia

William Candillon talks about where Redraw fits next to Skia, from physics-based rendering to camera effects, AR, and new UI patterns.

cover

Jun 29Article

Less Boilerplate, More Type Safety: React Navigation 8's Typed Hooks

React Navigation 8 improves typed hooks with screen-aware types, less boilerplate, and fewer unsafe casts in shared UI and nested navigators.

cover

Apr 30Live Stream

Build a React Native App With the TanStack Stack

Join Callstack’s live stream on building a React Native app from the ground up using the TanStack toolbelt.

cover

Apr 28Article

React Native production lessons from React Universe Meetup x Zalando

Lessons from React Universe Meetup x Zalando on React Native in production, covering brownfield performance, feed video, native API access, and on-device AI.

cover

Apr 23Talk

Shipping iOS Live Activities with React: The Voltra Story

How Voltra enables Live Activities and Dynamic Island views using React without writing Swift. A deep dive into iOS constraints and architecture.

cover

Apr 16Live Stream

React Native 0.85: Animation Backend and Key Updates

A recap of React Native 0.85, covering the new animation backend, DevTools improvements, and ecosystem takeaways.

cover

Feb 25Article

React Native Monorepo With pnpm Workspaces

Set up a React Native monorepo with pnpm workspaces for mobile and Next.js web, including Metro fixes, hoisted node-linker, and shared packages.

cover

Feb 18Article

A Practical Guide to React Native Monorepo With Yarn Workspaces

Learn how to set up a React Native monorepo with Yarn workspaces for mobile and web, including Metro, Gradle, and shared packages.

cover

Feb 17Podcast

From Teddy Bears to Voice Agents: Kraen Hansen on Voice AI, Local-First & App Security

What happens when you combine voice agents and teddy bears? A hands-on look at local-first systems, voice AI, and security lessons from poking real apps.

cover

Jan 28Article

Bringing CSS Clipping to React Native

A deep look at how a single CSS feature moves through React Native’s New Architecture, from JavaScript styles to native rendering on iOS and Android.

cover

Jan 13Article

Live Activities and Widgets with React: Say Hello to Voltra

Voltra lets you build iOS Live Activities and widgets for React Native using JSX instead of Swift.

cover

Mar 19Tutorial

The Hard Way vs. The React Native AI SDK Way: Stop Writing Custom Modules for Every Model

Learn how React Native AI uses a unified provider model to switch between cloud and on-device LLMs without rewriting application logic, enabling reliable AI experiences online and offline.

cover

Mar 12Tutorial

Build Smarter Apps: Tool Calling & AI Orchestration Explained

Learn how on-device models in React Native can call tools, interact with external APIs, and return structured outputs you can use directly in application logic.

cover

Mar 5Tutorial

MLC LLM + React Native: On-Device AI Without the Pain

Learn how to run third-party on-device LLMs in React Native using MLC LLM. Choose your models, run them efficiently across platforms, and keep a unified JavaScript API with React Native AI.

cover

Feb 24Tutorial

Intelligent Fallbacks with Apple Intelligence in React Native

Learn how to use Apple’s built-in Foundation Models in React Native apps, reduce memory usage, and enable fast, fully on-device AI with React Native AI Apple.

cover

Feb 10Tutorial

What Is the React Native AI SDK? A Complete Intro & Quickstart

Learn why on-device AI matters for React Native apps, how local LLMs behave offline, and what problems React Native AI is designed to solve from day one.

cover

Jan 29Tutorial

Implementing an Android TurboModule with Kotlin

Learn how to implement a React Native TurboModule on Android, from the TypeScript spec and Codegen to the Kotlin implementation and package registration.

cover

Jan 19Tutorial

Working with Different Threads in Swift TurboModules

Learn how JavaScript, native module, background, and main threads interact in React Native TurboModules, and how to use GCD queues to run async work safely in Swift.

cover

Jan 9Tutorial

How to Add Type-Safe Constants to Swift TurboModules

Learn how to expose typed, immutable constants from a Swift TurboModule using Codegen, and get full end-to-end type safety between native and JavaScript.

cover

Dec 30Tutorial

Adding Event Emitters to Your TurboModule in Swift

Learn how to emit native events from a Swift TurboModule and subscribe to them in JavaScript, including proper memory management and Codegen integration.

cover

Dec 22Tutorial

Writing Your First TurboModule in Swift

Learn how to build a fully working TurboModule in Swift, integrate it with Codegen, and bridge it through Objective‑C inside an Expo app.