Universal React Server Components in Expo Router & React Native

Elves Vieira
5 min readMay 16, 2024

--

Integrating React Server Components (RSC) with Expo Router marks a transformative advancement in React Native application development. This article will explore how these technologies work together to enhance application functionality and user satisfaction, pushing the boundaries of what’s possible in application development.

The Problem with Current Approaches

Let’s start with the hype: artificial intelligence. Applications like ChatGPT and Google Gemini handle extensive data and complex interfaces. These challenges can stifle interactive engagement, where responsiveness is crucial for maintaining user interest and satisfaction. Currently, these applications are confined to client-side components, severely limiting the ability of AI to vary the UI elements dynamically. For instance, while the server might want to offer an interactive calendar as a response, it’s constrained because it cannot send UI components directly to the client; it’s typically limited to sending static content like text and images.

User asking ChatGPT and Gemini to show new movies

What if we could optimize this process using server components that dynamically respond to and adapt based on user needs?

New approach: React Server Components in React Native

In response to the limitations of client-side only components, the introduction of Universal React Server Components marks a pivotal advancement. These components allow for the dynamic rendering of UI elements directly from the server, opening up possibilities for applications to adapt their interfaces in real-time based on user interactions.

User asking Expo AI App to show new movies and receiving native and interactive UI

Universal React Server Components operate differently from traditional client-side components. By rendering on the server, they can send fully interactive, dynamic UI components to the client. This means applications can now serve complex UI elements like interactive calendars, custom sliders, and more, directly in response to user queries. For example, imagine a marketplace application where each product could offer its own unique user experience, utilizing native components that are conceived and rendered by the server based on the product’s specific features and user interactions. Similarly, an AI could dynamically generate a personalized dashboard based on user data and preferences, which would be rendered server-side and sent to the client as a fully interactive interface.

Expo AI App generating several native and interactive components from the server based on user choices

This technology leverages the strengths of server-side processing — such as more powerful computation and centralized data handling — while mitigating the typical latency issues associated with client-side rendering. The result is a more efficient, responsive, and engaging user experience that previously could not be achieved with traditional methods.

But first, how do React Server Components Work on the web?

React Server Components revolutionize react ecosystem by shifting key rendering processes to the server. This approach harnesses the power of server-side rendering (SSR) to dynamically generate and update components based on real-time data. Instead of the traditional method where the server merely sends static HTML to the client, this model involves rendering interactive, stateful React components that adapt dynamically.

Once components are rendered on the server, updates and new components can be streamed to the client seamlessly. This streaming process significantly reduces initial load times and enhances responsiveness by delivering only the necessary updates, rather than reloading entire pages. As these components reach the client, they undergo a process known as hydration. This means the components are initially lightweight when they are transferred and then ‘hydrated’ with interactive functionality once they arrive. This method allows for rich interactivity without the burden of a heavy initial download, ensuring a smoother user experience.

Expo Router and Its Integration with React Server Components

Expo Router plays a crucial role in the seamless integration of React Server Components across both web and mobile platforms. As a file-based router designed for React applications, it not only simplifies the routing process but also enhances the dynamic capabilities of React Server Components. This integration allows developers to manage navigation and component rendering consistently across different platforms.

React Native using React Server Components

One of the groundbreaking features of this system its multiplatform adaptability. When a request is made from an iOS or Android device, or even from a web browser, the server recognizes the platform and renders the appropriate version of the server components. This means that the React Native text or React web elements are tailored to match the native feel and performance standards of each platform. Such capability ensures that the application not only looks right but also feels right, regardless of where it’s accessed from.

The process integrates server-side rendering with client-side interactivity seamlessly. Actions defined in the server code are extracted out of the bundle and turned into endpoints on the client. This uses Suspense to keep the stream open, allowing for continuous and dynamic updates. As components are rendered on the server, they are streamed to the client where they are “hydrated” with interactive capabilities. This streaming includes everything from the static representation of the root JSX to the URLs needed for the React Server Components to load, along with the currently rendered text elements.

As the stream progresses, more content is dynamically rendered, and React updates the UI tree with new visuals. This constant flow of updates enables applications to respond to user interactions in real time, providing a smooth and engaging user experience. A critical element of this architecture is the transmission of Hermes bytecode to the client, optimizing performance and ensuring compatibility with the Hermes engine used in React Native environments. This approach reduces the payload size and complexity, ensuring that applications remain fast and responsive.

Conclusion

The integration of Expo Router and React Server Components marks a significant milestone in the evolution of application development. This innovative approach not only streamlines the development process across web and native platforms but also significantly enhances the user experience by delivering dynamic, responsive, and interactive interfaces.

For developers and companies looking to stay at the forefront of technology, embracing these innovations offers a pathway to building more powerful, scalable, and user-centric applications. To stay updated with the latest developments and insights, follow Evan Bacon on Twitter, and watch the video of his talk at React Conf 2024 for a deeper understanding of these technologies.

Disclaimer: all images used in this article were provided by React Conf.

Feel free to contact me: LinkedIn

--

--

Elves Vieira

Computer Engineer with expertise in JavaScript. Expanding into C# via Asp.net and passionately exploring C++ and Python in my spare time.