简体   繁体   English

分享 React-Native 组件

[英]Share React-Native components

I am newbie in react, react-native and nodejs.我是 react、react-native 和 nodejs 的新手。

I tried create node module via npm init .我尝试通过npm init创建节点模块。 In this module i created a component - for start styled button.在这个模块中,我创建了一个组件 - 用于启动样式按钮。 I packed this via npm pack a link in application in package.json file by " file:../shared/_dist/shared-1.0.0.tgz " in dependency section.我通过依赖部分中的“ file:../shared/_dist/shared-1.0.0.tgz ”通过npm pack a link in package.json文件将其打包。

in my shared index.js is在我共享的 index.js 中是

import MyButtonFirst from './components/buttons/MyButtonFirst';
module.exports = { MyButtonFirst  };

in react application is在反应应用程序中是

import React from 'react;
import { MyButtonFirst } from 'shared';

export default function MySharedButton()
{
   return <MyButtonFirst />;
}

It works!有用!

Then i tried create component which using react-native-async-storage/async-storage (via npm install in shared project).然后我尝试创建使用react-native-async-storage/async-storage 的组件(通过共享项目中的 npm install )。 After increase version, npm pack, link and install new version of package I get error that AsyncStorage is null after android run.增加版本、npm 包、链接并安装新版本的包后,我得到错误,Android 运行后AsyncStorage 为空

Why AsyncStorage is null?为什么 AsyncStorage 为空? Have I create dependecy in both projects?我是否在两个项目中都创建了依赖关系? (that's a weird solution - it doesn't feel right to me, although it works) How to share for example resources like icons, images etc. (这是一个奇怪的解决方案 - 我觉得它不合适,虽然它有效)如何共享例如图标、图像等资源。

We need to develop three applications on the same data (API) in the field of sports for different types of users (athlete, referee, administrator of the sports ground) and a lot of code we need to share - icons, contexts (user, theme etc...), error handling, API calls etc... We don't want develop it as one big rights-controlled application, but as several small applications for individual roles.我们需要为不同类型的用户(运动员、裁判、运动场管理员)开发三个基于体育领域相同数据(API)的应用程序以及我们需要共享的大量代码——图标、上下文(用户、主题等...)、错误处理、API 调用等...我们不希望将其开发为一个大型的权限控制应用程序,而是开发为针对各个角色的几个小应用程序。

What is the best way how to share code between more react-native apps?如何在更多本机应用程序之间共享代码的最佳方式是什么?

AsyncStorage is deprecated, see here . AsyncStorage已弃用,请参见此处

You could create a start.js that links to different apps based on the feedback of your database (the roles of your users).您可以根据数据库的反馈(用户的角色)创建一个链接到不同应用程序的 start.js。 Else, it will route to a welcome component with, for example, a login and registration child-component.否则,它将路由到带有例如登录和注册子组件的欢迎组件。

import ReactDOM from "react-dom";
import Welcome from "./welcome";
import AppAth from "./app-ath";
import AppRef from "./app-ref";
import AppAdmin from "./app-admin";

fetch("api/users/role.json")
    .then((res) => res.json)
    .then((user_role) => {
        if (user_role == "ath") {
            ReactDOM.render(<AppAth />, document.getElementById("root"));
        } else if (user_role == "ref") {
            ReactDOM.render(<AppRef />, document.getElementById("root"));
        } else if (user_role == "admin") {
            ReactDOM.render(<AppAdmin />, document.getElementById("root"));
        } else {
            ReactDOM.render(<Welcome />, document.getElementById("root"));
        }
    })
    .catch((err) => console.log(err));
            

Like that, you can keep the standard folder tree of an application in React and share all child-components, hooks and files in the public folder between them:像这样,您可以在 React 中保留应用程序的标准文件夹树,并在它们之间共享公共文件夹中的所有子组件、挂钩和文件:

在此处输入图像描述

To keep the users separate, you store the role of the user in a cookie and check for the role on the server side.为了使用户分开,您将用户的角色存储在 cookie 中并在服务器端检查角色。

If the cookie is empty, it will always lead back to the welcome component.如果 cookie 为空,它将始终返回到欢迎组件。

Side note: of course, the folder structure is always dependent on the bundler setup!旁注:当然,文件夹结构始终取决于捆绑程序设置! So the folder structure of your app could differ from the one on the image.因此,您的应用程序的文件夹结构可能与图像上的不同。

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

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