react native 全局变量global的使用问题

看很多问这个,很多回答是这样写 global.test = 1; 在别的页面就可以拿到,我就在index 里面这样写了,报错啊?在用global之前真的不用做的别的操作吗?求 完整的!使用方法

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
Button,
AsyncStorage
} from 'react-native';

import ForNav from './pages/ForNav.js';

export default class One extends Component {
constructor(props) {

super(props);
**global.test = 1;**  

}
render() {

return (
  <ForNav />
);

}
}

const styles = StyleSheet.create({
container: {

flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',

},
welcome: {

fontSize: 20,
textAlign: 'center',
margin: 10,

}
});

AppRegistry.registerComponent('demo', () => One);

阅读 14.1k
2 个回答

可以把需要全局的global写在配置文件里面 导入一次就可以了。我一直是这么写的。

global.test = 1; **的写法是错的吧……

直接 global.test = 1 就好了。

不过不推荐加东西,直接用 redux实现全局数据

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题