我正在尝试通过AppContainer通过道具。我能够通过其他组件,但是我不知道如何通过CreateAppContainer发送道具
在app.js中:
render() {
return (
this.state.isLoggedIn ? <DrawerNavigator /> :
<SignedOutNavigator handler={this.saveUserSettings} />
)
}
在签名navigator中:
import React from "react";
import { View, Text } from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import LoginScreen from "../screens/LoginScreen";
const SignedOutNavigator = createStackNavigator({
Login: {
// screen: LoginScreen
screen: props => <LoginScreen screenProps={value => {
// I need to access props from here
// like this.props.handler(value)
}} />,
navigationOptions: ({ navigation }) => ({
header: null,
}),
}
});
export default createAppContainer(SignedOutNavigator);
您必须在screenProps
下范围范围范围,以便能够在screen
级别访问它。
// App.js
<AppNavigator
screenProps={{
handler: () => {},
hello: "World"
}}
/>
// Navigator.js
const StackNavigator = createStackNavigator({
Login: {
screen: ({ screenProps, navigation }) => {
screenProps.handler();
// ...
},
},
})
好吧,我在塞缪尔·瓦兰特(Samuel Vaillant)的帮助下工作了。我必须进行几个修改。
我的app.js:
render() {
return (
this.state.isLoggedIn ? <DrawerNavigator /> : <SignedOutNavigator
screenProps={{
handler: (settings) => { this.saveUserSettings(settings) }
}}
/>
)
}
我的签名navigator:
import React from "react";
import { View, Text } from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import LoginScreen from "../screens/LoginScreen";
const SignedOutNavigator = createStackNavigator({
Login: {
// screen: LoginScreen
screen: screenProps => <LoginScreen screenProps={value => {
screenProps.screenProps.handler(value)
}} />,
navigationOptions: ({ navigation }) => ({
header: null,
}),
}
});
export default createAppContainer(SignedOutNavigator);