Building a universal React app with Expo, Next.js & Nativewind
Build a universal React application that works seamlessly across mobile and web platforms using Expo, Next.js, and NativeWind in a monorepo setup.
Introduction
Building universal React applications has never been easier or more efficient, thanks to Expo. Expo is a powerful toolchain that simplifies the development process, allowing developers to create high-quality, performant apps for iOS, Android, and the web with a single codebase.
With this guide, we will set up a monorepo from scratch to build a Universal React app using Expo and Next.js using tools like NativeWind/Tailwind, Turborepo for building apps across both mobile and web platforms.
Problem
At my job I was assigned the task of building a design system for both our mobile and web products. Given my background as a React developer, React Native was the natural choice for mobile development.
The challenge was to create a shared component library with consistent styling that works seamlessly across both mobile and web applications using React and React Native.
The goal was to develop a solution that supports the development of both mobile and web applications without duplicating components, rewriting business logic, or maintaining separate codebases.
Before we get started, let's familiarise ourselves with some key terminologies.
Universal in this case means it works on all platforms i.e Andriod, IOS, Web and others.
Expo
Expo is a framework that makes developing Android and iOS apps easier
Next.js
Next.js is a React framework for building full-stack web applications.
React Native for Web
Makes it possible to run React Native components and APIs on the web using React DOM.
Prerequisites
- Node.js (
>=18
) - Yarn (
v1.22.19
) - Native Development Environment (Xcode, Android Studio e.t.c)
Setup yarn workspaces
We need to initialise our project with a package.json file
Using Classic yarn as Expo documentation recommends it.
We currently have first-class support for Yarn 1 (Classic) workspaces. If you want to use another tool, make sure you know how to configure it.
Set private flag as true
Note that the private: true is required, Workspaces are not meant to be published.
Create sub folders apps
and packages
packages/* simply means we'll reference all packages from a single directive
apps contains
- web
- native
packages contains
- ui
- utils e.t.c
Install Turborepo
turbo
is built on top of Workspaces, a feature of package managers in the JavaScript ecosystem that allows you to group multiple packages in one repository.
Add turbo.json
file
Update .gitignore
file
Setup Default Typescript config in the root workspace
Setting up Packages
Create new shared packages for the monorepo in packages
folder containing
ui
app
cd into packages/ui
run
Next, create an empty index.ts
in packages/ui file for now
Structure of Monorepo
Setup default apps
for native and web with Expo & Next.js
Navigate to the apps directory
Setting up Nextjs app
Run
Update tsconfig.json to include
Using Expo
You'll be prompted to enter your app name. Set your app name as native
and run the command to reset it as fresh project
Optionally, you could delete the boilerplate files and folders generated from create-expo-app
- /app-example.
- components
- hooks
- constants
- scripts
replace tsconfig.json in the native
folder
Ensure you have expo-env.d.ts
file
To run your project, navigate to the directory and run one of the following commands.
Setting Up React Native Web in Next.js app
In the root directory, add resolutions to package.json file
In apps/web Run
Updating Next.js Configuration
Edit next.config.js
Resetting React Native Web styles
The package react-native-web
builds on the assumption of reset CSS styles, here's how you reset styles in Next.js
Add to globals.css
Creating first shared component
Now we have RNW(React Native Web), Let's write our first shared component.
Create a file view/index.tsx
and in the ui
package
Update the packages/ui/index.ts
Add
Using ui package
Add ui
package in both native and web dependencies in package.json file
Replace apps/native/index.tsx
in Expo app
Replace apps/web/index.tsx
in Next.js app
Configuring Metro bundler
To configure a monorepo with Metro manually, there are two main changes:
Wee need to make sure Metro is watching all relevant code within the monorepo, not just apps/native
.
cd apps/native
npx expo customize metro.config.js
Update metro.config.js
Update default entry point for Expo app
Update main field in package.json in apps/native
Now, we have a working native and web app using shared component with RNW
Results
Universal Styling with NativeWind
Next, we want to further and style both platform using Tailwind in Next.js and on mobile, NativeWind is the right tool to achieve.
NativeWind allows you to use Tailwind CSS to style your components in React Native. Styled components can be shared between all React Native platforms.
Run pod-install
to install Reanimated pod:
Run npx tailwindcss init
to create a tailwind.config.js file
Add the paths to all of your component files in your tailwind.config.js file.
Create a CSS file global.css
and add the Tailwind directives
Copy & paste in global.css
file
Add babel preset
Configure babel to support NativeWind
Modify Metro Config
Import your CSS file
In the app/layout.tsx
file
Typescript Support
NativeWind extends the React Native types via declaration merging. Add triple slash directive referencing the types. Add a file app-env.d.ts
in apps/native
root directory.
Next.js Support
Update tailwind.config.js in apps/web
Update tsconfig.json file
Using Nativewind in shared UI
In packages/ui
Update the view
component we created earlier
Finally add nativewind
to list of packages to transpile
Update the use of the ui/view
component in both web and mobile app
Now, Using className
works just same as with web.
VSCode Intellisense Support
Create a new file tailwind.config.ts
in the project root directory and paste the following in the file.
Troubleshooting
if you're using typescript, confirm you have the reference to NativeWind types in app-env.d.ts
Happy to help with any issues, be sure to leave a comment if you need help or found this useful.
Links
Closing
Next, you'll need to build your own or custom universal components. I'll recommend React Native Reusables to get started with most basic components.
If you're interested in using an existing template, I've followed the steps from this guide to create a starter template on Github.