简体   繁体   English

React Native是否具有全局范围?如果没有,我怎么能模仿呢?

[英]Does React Native have global scope? If not, how can I mimic it?

I haven't been able to find anything like 我找不到类似的东西

$rootScope

for React Native and I want to share a couple variables between different Views. 对于React Native,我想在不同的视图之间共享几个变量。

The global scope in React Native is variable global. React Native中的全局范围是可变全局的。

global.foo = foo

Then you can use global.foo anywhere. 然后你可以在任何地方使用global.foo

But do not abuse it! 但是不要滥用它! In my opinion, global scope may used to store the global config or something like that. 在我看来,全局范围可能用于存储全局配置或类似的东西。 Share variables between different views, as your description, you can choose many other solutions(use redux,flux or store them in a higher component), global scope is not a good choice. 在不同视图之间共享变量,作为您的描述,您可以选择许多其他解决方案(使用redux,flux或将它们存储在更高的组件中),全局范围不是一个好的选择。

A good practice to define global variable is to use a js file. 定义全局变量的一个好习惯是使用js文件。 For example global.js : 例如global.js

global.foo = foo;
global.bar = bar;

Then, to make sure it is executed when project initialized. 然后,确保在项目初始化时执行它。 For example, import the file in index.js : 例如,在index.js导入文件:

import './global.js'
// other code

Now, you can use the global variable anywhere, and don't need to import global.js in each file. 现在,您可以在任何地方使用全局变量,而不需要在每个文件中导入global.js。 Try not to modify them! 尽量不要修改它们!

For example, you can export class with static properties and then import there when need. 例如,您可以使用静态属性导出类,然后在需要时导入。

In main.js for example: 在main.js例如:

export class AppColors {
    static colors = {main_color: "#FFEB3B", secondary_color: "#FFC107"}
}

In other file, where need to get this colors 在其他文件中,需要获取此颜色

import { AppColors } from './main.js';

class AnotherPage {
    constructor() {
        super();
        console.log(AppColors.colors); // youla! You will see your main and secondary colors :)
    }
}

Storing variables in the global scope is a bit of an anti-pattern in React. 在全局范围内存储变量在React中是一种反模式。 React is intended to be used with a one-way data flow, meaning data flows down from components to their children. React旨在与单向数据流一起使用,这意味着数据从组件流向其子节点。 This is achieved through the passing of props . 这是通过道具的传递来实现

In order to share a value between multiple views, you would either need to store the data on a shared parent component and pass it down as a prop OR store it in a separate entity that is responsible for data management. 为了在多个视图之间共享一个值,您需要将数据存储在共享父组件上,并将其作为prop传递或将其存储在负责数据管理的单独实体中。 In ReactJS this is achieved through patterns like Flux , but I'm not sure about the options for react-native. 在ReactJS中,这是通过像Flux这样的模式实现的,但我不确定反应原生的选项。

Step 1. create ConstantFile.js File and put this code. 步骤1.创建ConstantFile.js文件并放入此代码。

export class ConstantClass {
    static webserviceName = 'http://172.104.180.157/epadwstest/';
    static Email = 'emailid';
    static Passoword = 'password';
}

Step 2. Access as like below. 步骤2.访问如下。 But before that, you need to import your js file to particular class file something like that 但在此之前,您需要import js文件import到类似的特定类文件中

import { ConstantClass } from './MyJS/ConstantFile.js';

ConstantClass.webserviceName

ConstantClass.Email

ConstantClass.Passoword

You can store those values in parent components state. 您可以将这些值存储在父组件状态中。 Then pass methods changing those values via props or context / or like @Jakemmarsh suggested - use Flux / Redux approach. 然后传递方法通过道具或上下文/或类似@Jakemmarsh建议改变这些值 - 使用Flux / Redux方法。

OR, you can use AsyncStorage . 或者,您可以使用AsyncStorage Its useful for storing app data like tokens and such. 它可用于存储令牌等应用程序数据。

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

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