Welcome to FullStack. We use cookies to enable better features on our website. Cookies help us tailor content to your interests and locations and provide other benefits on the site. For more information, please see our Cookies Policy and Privacy Policy.
Live demos allow developers to preview components without requiring them to go through the manual process of downloading, installing, and displaying each component individually.
While Snack.expo.io is a great service that can render react-native on the web, it can be frustratingly slow and unreliable. Instead, we can render react-native components with react-native-web, and use react-live to allow live editing. React-live loads faster, is more reliable, and allows much more control over both the editor and the component preview.
The app should be running on port 8080. If there are no errors, “Hello World” will be displayed on the page.
Note: The key to running react-native on the web is found in the webpack.config.js file. The code below tells webpack to replace all instances of react-native with react-native-web components.
...
**resolve: {
/* auto resolves any react-native import as react-native-web */alias: {
"react-native": "react-native-web" },
extensions: [".web.js", ".js"]
},**
...
You should now be able to add react-native components to App.js. However, not all react-native components are supported yet. All supported components are listed on this page.
Create a component to demo
We'll need a component to display in our live demo, so let's recreate this image of Toad using React Native.
We can recreate Toad using only <view></view> components. So first, we need to import <view></view> at the top of App.js.
Next, comment out the Toad code and add the following to the render method below the commented out Toad code:
<liveprovider code="<strong>This is a live Component</strong>"></liveprovider>
<livepreview></livepreview><liveeditor></liveeditor><liveerror></liveerror>
You should see the bolded "This is a live Component" text with the JSX below it. If you edit the JSX, the text or component will update in real time. The <liveprovider></liveprovider> simply takes a string of JSX in the code prop and renders that JSX in the <livepreview> </livepreview>component. The <liveeditor> </liveeditor>component displays the string of JSX as JSX. Finally, the <liveerror></liveerror> component will show any errors that occurred.
Render Toad Live
Uncomment the Toad code from above, copy it, and create a string at the top of the page with the full Toad code. Add the Toad styles above the JSX:
There should be a syntax error shown at the bottom of the page within the <liveerror></liveerror> component because react-live is trying to evaluate the JSX inline and running into a style object. To fix this, first add the noInline prop to the <liveprovider></liveprovider>:
Scrolling to the bottom of the page again you will see another error “ReferenceError: View is not defined”. By default, react-live does not have references to react-native components, so we need to provide react-live with a reference to both <view> and StyleSheet to render properly. We can do this with the scope prop which allows the code passed into the code prop access variables and components.</view>
You should see Toad nested inside the <livepreview> and all the Toad JSX and styles below. Since this is a live component, you can change the styles or JSX see them update in real time. Try changing Toad's colors or head size, and watch the image update in real time. Check out the react-live docs to learn how to customize the LiveEditor and LivePreview.
---
At FullStack Labs, we pride ourselves on our ability to push the capabilities of cutting-edge frameworks like React Native. Interested in learning more about using React Native on the web? Contact us.
What is the purpose of creating live React Native web demos?
Live demos allow developers to preview and test React Native components directly in the browser without downloading or installing the project locally. They make it easier to interact with components, see real-time changes, and share examples with stakeholders or other developers.
Why use React Live instead of Snack.expo.io?
While Snack.expo.io supports rendering React Native components on the web, it can be slow and less reliable. React Live, combined with React Native Web, provides faster performance, better reliability, and more control over the editor, live preview, and overall demo environment.
How does React Native Web enable React Native components to run in the browser?
React Native Web maps React Native components like View and Text to standard web components under the hood. In the setup, the webpack.config.js file is configured to alias react-native imports so they resolve to react-native-web, making it possible to run React Native components in a browser environment.
What is React Live, and how does it work?
React Live is a library that enables live, editable component previews. Developers can modify JSX code in the built-in editor, and the changes instantly appear in the preview. It works by wrapping components with the LiveProvider, which renders the live LiveEditor, LivePreview, and LiveError components for real-time interaction and feedback.
Can React Live be used with custom React Native components and styles?
Yes. By combining React Live with React Native Web, you can display custom components and styles in a live environment. You’ll need to pass the relevant React Native components (e.g., View, StyleSheet) into the scope of the LiveProvider so that React Live can reference them correctly and render the components in the browser.
AI is changing software development.
The Engineer's AI-Enabled Development Handbook is your guide to incorporating AI into development processes for smoother, faster, and smarter development.
Enjoyed the article? Get new content delivered to your inbox.
Subscribe below and stay updated with the latest developer guides and industry insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.