Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Web with Next JS and React Navigation

I am trying to decide on the best way to set up routing in my React Native Web project. I am using expo and followed this guide to use Next JS https://docs.expo.io/versions/latest/guides/using-nextjs/ so I have App.js like this:

import index from "./pages/index";
import alternate from "./pages/alternate";
import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer } from "react-navigation";
const AppNavigator = createStackNavigator(
  {
    index,
    alternate
  },
  {
    initialRouteName: "index"
  }
);

const AppContainer = createAppContainer(AppNavigator);
export default AppContainer;

My concern is how best to handle routing. I have my index.js page setup like this currently.

import * as React from 'react'
import { StyleSheet, Button, Text, View } from 'react-native'


export default function App ({navigation}) {
  return (
    <View style={styles.container}>

      {/* Native route */}
      <Button
        title="Go to Details"
        onPress={() => navigation.navigate("alternate")}
      />

      {/* Web route */}
      <Text style={styles.link} accessibilityRole="link" href={`/alternate`}>
        A universal link
      </Text>
    </View>
  );
}

As you can see this is currently requiring separate code to render a Native vs Web route. I am wondering what is the best way to handle this sort of rendering. I looked into using React Navigation for web and wouldn't be opposed to this but it seems like I should probably stick with the Next Router.

Thanks in advance for any advice on handling conditional rendering like this.

like image 916
Mike Avatar asked Nov 18 '19 05:11

Mike


People also ask

Can you use React Native with Nextjs?

so if you want to build a web application that runs on the browser you can use react or next, Next has added some nice features like server-side rendering and the ability to have js backend on the project too. and you can use React Native when you want to produce a native application.

Does React Native Navigation work on web?

"React Native for Web" makes it possible to run React Native components and APIs on the web using React DOM. This approach allows you to reuse most of React Navigation on the web because React Native for Web maps React Native primitives like View , Text , and others to their equivalents on the web.

Can we use react Navigation in React Native?

If you're already familiar with JavaScript, React and React Native, then you'll be able to get moving with React Navigation quickly!

Can you use next JS and react together?

Next. js is a widely-used framework for building React applications that offer server-side rendering, automatic code-splitting, static exporting options, and easy production builds. It also relieves a lot of the general headaches involved with creating production-ready React applications.

Can I use @React-navigation/web with React Native for web?

Note: Unlike React Navigation 4, you don't need to install a separate package to use web integration when using React Native for Web. If you have the @react-navigation/web package installed, please uninstall it because it cannot be used with React Navigation 6.

Is it possible to use nextjs in react navigation?

For anyone who want to know, NextJS features are not compatible with Navigation system in React Navigation. However, Next can still be used aside of RNav and DrawerNavigator.

What is React-Native-stack navigator?

Native stack navigator that uses native navigation primitives for navigation using react-native-screens New backends for Material top tab navigator based on react- native-viewpager and ScrollView What is React Native Navigation? React Native Navigation is a popular alternative to React Navigation.

How do I start a React Native project with yarn?

Assuming you have Yarn installed, the first step is to set up a React Native app. The easiest way to get started with React Native is with Expo tools because they allow you to start a project without installing and configuring Xcode or Android Studio. Install Expo by running this: If you encounter any error on Mac, try running it this way:


2 Answers

Use reactnavigation web support for that

https://reactnavigation.org/docs/en/web-support.html

import { createSwitchNavigator } from "@react-navigation/core";
import { createBrowserApp } from "@react-navigation/web";

const MyNavigator = createSwitchNavigator(routes);

const App = createBrowserApp(MyNavigator);

// now you can render "App" normally
like image 138
Alex Lévy Avatar answered Nov 15 '22 04:11

Alex Lévy


There is import { Platform } from 'react-native':

{Platform.OS === 'web' ? (
  <Text
    style={styles.link}
    accessibilityRole="link"
    href={`/alternate`}
  >
    A universal link
  </Text>
) : (
  <Button
    title="Go to Details"
    onPress={() => navigation.navigate("alternate")}
  />
)}
like image 30
5ervant - techintel.github.io Avatar answered Nov 15 '22 04:11

5ervant - techintel.github.io