简体   繁体   English

你能在 React Native App 中使用 Next.js 吗?

[英]Can you use Next.js with a React Native App?

I have recently been learning Next.js for React and I like the routing system a lot more than react navigation.我最近一直在学习 Next.js 的 React,我更喜欢路由系统而不是 React 导航。 Now I am about to start a react native frontend and I was wondering if you can utilise Next.js's routing system in react native as well as it's other benefits.现在我要开始一个 React Native 前端,我想知道你是否可以在 React Native 中利用 Next.js 的路由系统以及它的其他好处。 I know that you can use it with expo but I am a little confused as to how it all works and I am starting to wonder if the Next only works on the web side of things..我知道你可以将它与 expo 一起使用,但我对它的工作原理有点困惑,我开始怀疑 Next 是否只适用于 web 方面的事情..

Does anyone have any experience with this?有人对这个有经验么? Any information would be much appreciated.任何信息将不胜感激。

Thanks!谢谢!

Unfortunately you can't do such thing, due to the fact that the views of react native are Equivalent to native views in Java or Objective C. When you get build version of react native, you are receiving Java or Objective C native views thank to react native's RN bridge .不幸的是,你不能做这样的事情,因为 react native 的视图等同于 Java 或 Objective C 中的原生视图。当你获得 react native 的构建版本时,你将收到 Java 或 Objective C 原生视图,这要归功于反应本机的RN 桥 Meanwhile, when you get build version of your Next.js project, you are going to receive HTML, CSS, JS and these codes are not familiar to native platforms.同时,当您获得 Next.js 项目的构建版本时,您将收到 HTML、CSS、JS,这些代码对于原生平台来说并不熟悉。

However, you can use your Next.js backend as your endpoints and fetch data in react native via calling this endpoints.但是,您可以使用 Next.js 后端作为端点,并通过调用此端点在 React Native 中获取数据。

UPDATE Oct_2022: 2022 年 10 月更新:

there seem to be a new tool created for this purpose, called solito .似乎有一个为此目的创建的新工具,称为solito I haven't used it personally but it might meet your needs.我没有亲自使用过它,但它可能会满足您的需求。

You can check out Tauri .您可以查看Tauri With that you can create from Next.js, React, Svelte etc. cross-platform native Apps.有了它,您可以从 Next.js、React、Svelte 等创建跨平台原生应用程序。 Currently, Windows, Mac and Linux are supported stable.目前稳定支持 Windows、Mac 和 Linux。 Android & iOS are currently in Alpha and will be probably released the next weeks. Android 和 iOS 目前处于 Alpha 阶段,可能会在接下来的几周内发布。

The magic behind Tauri is, that they provide around your Frontend library/framework a Rust layer with that you can interact with platform native APIs. Tauri 背后的神奇之处在于,它们围绕您的前端库/框架提供了一个 Rust 层,您可以通过它与平台原生 API 进行交互。 They call it Commands .他们称之为命令

You can find the guide for Next.js here .您可以在此处找到 Next.js 的指南。

Also, if it's not exactly answering the question - "Is it possible to use Next.js in React native ?"此外,如果它没有完全回答问题 - “是否可以在React native中使用 Next.js?” - I hope that it's helping everyone who is searching for a solution to use Next.js as Cross-Platform Frontend Framework - 我希望它能帮助所有正在寻找使用 Next.js 作为跨平台前端框架的解决方案的人

I read a thread on ycombinator on this where Solito was mentioned and tried it out.我在 ycombinator 上阅读了一个线程,其中提到了 Solito 并尝试了它。 In the comments I found this, which suited my needs best:在评论中我发现这个最适合我的需要:

https://github.com/mlynch/nextjs-tailwind-ionic-capacitor-starter https://github.com/mlynch/nextjs-tailwind-ionic-capacitor-starter

Solito looks nice but the all in one repo was using Expo, which I did not like because it installs an extra app. Solito 看起来不错,但一体化存储库使用的是 Expo,我不喜欢它,因为它安装了一个额外的应用程序。 I would check it out for yourself and look at the repo above too, which is using Capacitor and is aimed at having one codebase for the two apps.我会亲自检查并查看上面的回购协议,它使用 Capacitor 旨在为两个应用程序提供一个代码库。

Both examples worked well.这两个例子都运作良好。 For Tauri I could not find the mobile support so I didn't try it.对于 Tauri,我找不到移动支持,所以我没有尝试。 I would be interested to read more about people's experience with running a minimum stack for cross platform development.我有兴趣阅读更多有关人们为跨平台开发运行最小堆栈的经验。

Note: Solito uses React Native, the repo uses Capacitor instead, which may also suite your needs because it works on both iOS and Android.注意:Solito 使用 React Native,repo 使用 Capacitor,这也可能满足您的需求,因为它适用于 iOS 和 Android。

There's a lot of support now for this tech stack.这个技术堆栈现在有很多支持。 Besides solito , which is already mentioned, there is also https://tamagui.dev/ which tries to solve all the issues with react-native-web and Next.js along with Solito.除了已经提到的solito之外,还有https://tamagui.dev/试图解决 react-native-web 和 Next.js 以及 Solito 的所有问题。

While they currently recommend a monorepo (which adds a lot of complexity in my opinion), I'm playing around with a vanilla React Native + Next.js here: https://github.com/criszz77/luna虽然他们目前推荐使用 monorepo(在我看来这增加了很多复杂性),但我正在使用 vanilla React Native + Next.js 在这里: https://github.com/criszz77/luna

This command should get you started to play around:这个命令应该让你开始玩:

npx react-native init MyAppName --template @criszz77/luna

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM