react-native - 将从一个屏幕分配的变量传递到另一个屏幕
问题描述
基本上我需要将一个名为 tempRole 的参数从 Login 传递到 MainTabNavigator,并根据用户角色创建相应的选项卡。例如,供应商有 4 个选项卡,而其他只有 3 个选项卡。但是,我似乎无法通过这个角色。
从登录
import React from 'react';
import { Text, View, StyleSheet, Platform, TextInput, TouchableOpacity } from 'react-native';
import firebase from 'firebase';
import { Container, Form, Item, Label, Input, Button } from "native-base";
import * as FirebaseAPI from '../modules/firebaseAPI';
import MainTabNavigator from '../navigation/MainTabNavigator';
import bottomTabNavigator from '../navigation/MainTabNavigator';
export default class LoginScreen extends React.Component {
constructor(props) {
super(props);
}
static navigationOptions = {
title: 'Login',
};
state = {
LoginEmail: "",
LoginPassword: "",
};
componentDidMount() {
this.watchAuthState(this.props.navigation)
try {
window = undefined;
} catch (e) {
}
}
watchAuthState(navigation) {
firebase.auth().onAuthStateChanged(function(user) {
console.log('onAuthStateChangedLOGIN: ', user)
if (user) {
// user.displayName will be like vendor.Peter
// e.g. role.name
var tempName = user.displayName;
navigation.navigate('Main', {
userRole: tempName.substr(0,tempName.indexOf('.'))
});
}
});
}
signIn(LoginEmail, LoginPassword) {
FirebaseAPI.signInUser(LoginEmail, LoginPassword);
}
render() {
return (
<Container style={styles.container}>
<Form>
<Text style={styles.text}>Login</Text>
<Item style={styles.standardDefaultInput} floatingLabel>
<Label style={{textAlign: 'center'}}>Email (example@example.com)</Label>
<Input
autoCapitalize="none"
style={{textAlign: 'center'}}
autoCorrect={false}
onChangeText={(text) => this.setState({LoginEmail: text})}
value={this.state.LoginEmail}
/>
</Item>
<Item style={styles.standardDefaultInput} floatingLabel>
<Label style={{textAlign: 'center'}}>Password (min. 6 charatcers)</Label>
<Input
autoCapitalize="none"
style={{textAlign: 'center'}}
autoCorrect={false}
onChangeText={(text) => this.setState({LoginPassword: text})}
value={this.state.Password}
/>
</Item>
<Button style={styles.standardDefaultButton} onPress={() => this.setState(this.signIn(this.state.LoginEmail, this.state.LoginPassword))} full rounded success>
<Text>Log In</Text>
</Button>
<Button style={styles.standardDefaultButton} onPress={() => this.props.navigation.navigate('SignUp')} full rounded link>
<Text>Sign Up</Text>
</Button>
</Form>
</Container>
);
};
}
请注意,到 Main 的导航是到 TabNavigator
从 MainTabNavigator
let bottomTabNavigator = null
//let user = navigation.getParam(user)
//let userRole = user.displayName.substr(0,user.displayName.indexOf('.'))
//const { navigation } = this.props;
// The above failed
let userRole = navigation.getParam('userRole');
if (userRole == "vendor") {
bottomTabNavigator = createBottomTabNavigator({
HomeStack,
ListingStack,
CalendarStack,
ProfileStack
});
} else {
bottomTabNavigator = createBottomTabNavigator({
HomeStack,
CalendarStack,
ProfileStack
});
}
export default bottomTabNavigator
解决方案
使用全局函数传递值。
当您需要将数据传递到不是父子关系的屏幕时,全局函数很有用。
全局功能画面:
let NICKNAME = "";
function setNickName(data) {
NICKNAME = data;
}
function getNickName() {
return NICKNAME;
}
export {setNickName,getNickName }
发送数据屏幕:
import {setNickName} from "GlobalFunctionScreen"
...
this.state={
data: "sendData"
}
...
componentDidMount(){
setNickName(this.state.data);
}
接收数据屏幕:
import {getNickName} from "GlobalFunctionScreen"
...
componentDidMount(){
data = getNickName();
alert(data);
}
推荐阅读
- reactjs - React 组件:出现语法错误:尝试将函数添加到我的代码时无法读取未定义的属性(读取“地图”)(0:未定义)
- vba - 如果满足特定条件,则循环遍历范围并删除行
- python - 如何按列中的类别聚合以填充另一列中的空值?(Python)
- vue.js - jsPDF未定义
- angular - 如何从component.ts触发p-tree中的(onNodeSelect)事件?
- linux - 如何在linux中将两个bash调用合并为一个
- python - 合并熊猫数据框中的两列,但按特定顺序
- pandas - Pandas 使用带有返回多个变量的函数的 Rolling Apply
- html - 将内容居中到父级,左侧有框
- azure - 使用脚本的 Azure Synapse 数据转换