Unlock the Power of Cross-Platform Development with React Native for Web
React Native for Web is a game-changing open-source project that allows you to harness the power of React Native core components in your React web applications. By leveraging React DOM, you can render JavaScript code compatible with React Native in a web browser, making code sharing a breeze.
Getting Started with React Native
To begin, let’s create a new React Native app using npx or the react-native-cli package. Open a terminal window and execute the following command: npx react-native init MyApp
(replace “MyApp” with your project name). Once the build process is complete, you’ll see the default React Native app with boilerplate code, which you can modify inside of App.js.
Setting Up Parcel for Web Development
Next, we’ll use Parcel, a zero-configuration bundler, to set up a new web app in React. Create a new directory called web/
inside your React Native project, and add the following files:
index.html
:<html><body><h2 id="root"></h2></body></html>
App.js
:import React from 'eact'; function App() { return <h2>Web App Title</h2>; } export default App;
index.js
:import React from 'eact'; import ReactDOM from 'eact-dom'; import App from './App'; ReactDOM.render(<App />, document.getElementById('root'));
Configuring Parcel and Adding Dependencies
Initialize a React app with yarn init --yes
, and install the required dependencies: yarn add react react-dom react-native-web
. Add the following scripts to your package.json
file: "start": "parcel index.html"
. Configure Parcel to add an alias for the react-native-web
package by adding the following code to package.json
: "aliases": {"react-native-web": "./node_modules/react-native-web"}
.
Creating a Shared Component
Now that we’ve set up both React Native and React web apps, let’s create a shared component that can be used in both. Inside the root directory of your project, create a new subdirectory called shared/components
. Create a file called Header.js
with the following code: import { View, Text } from 'eact-native'; export function Header() { return <View><Text>Header Title</Text></View>; }
.
Sharing Components Between Mobile and Web Apps
Add the Header
component to the App.js
file in the root directory of your project, and create a symbolic link to the shared
directory for the web app: ln -s../shared shared
. Now, you can import the Header
component inside the web/App.js
file: import Header from '../shared/components/Header';
.
Inspecting React Native for Web in Action
Run yarn start
to restart the development server for the web app. Open up Developer Tools and navigate to the Elements tab to see how React Native for Web builds the DOM elements in the web browser. You’ll notice that React Native for Web converts React Native styles and components to DOM elements, using JavaScript objects to style components.
Take Your React Native Apps to the Next Level
Over 200,000 developers use LogRocket to create better digital experiences. LogRocket is a React Native monitoring solution that helps you reproduce issues instantly, prioritize bugs, and understand performance in your React Native apps. Try LogRocket for free today!