以下代码在我的Expo React本机项目中正常工作:
const AppNavigator = createSwitchNavigator({
Loading:Loading,
Auth: AuthenticationNavigator,
Main: MainNavigator
});
const AppContainer = createAppContainer(AppNavigator);
class App extends React.Component {
render() {
return <AppContainer/>
}
}
我想将screenProps
添加到我的createSwitchNavigator
。我使用了这个示例。这是我的代码:
const AppNavigator = createSwitchNavigator({
Loading:Loading,
Auth: AuthenticationNavigator,
Main: MainNavigator
});
const AppNavigatorWithProps=(<AppNavigator screenProps={{testing:true}} />)
const AppContainer = createAppContainer(AppNavigatorWithProps);
class App extends React.Component {
render() {
return <AppContainer/>
}
}
我有以下错误:
TypeError: undefined is not an object (evaluating 'Component.router.getStateForAction')
This error is located at:
in NavigationContainer (at App.js:42)
in App (at withExpoRoot.js:22)
in RootErrorBoundary (at withExpoRoot.js:21)
in ExpoRootComponent (at renderApplication.js:34)
in RCTView (at View.js:44)
in RCTView (at View.js:44)
in AppContainer (at renderApplication.js:33)
我在做什么错?
class App extends React.Component {
render() {
return <AppContainer screenProps={{ testing: true }} />
}
}
喜欢这个
您不需要用createAppContainer
或其他任何东西将AppNavigator
包装
只需使用周dai答案如下:
class App extends React.Component {
render() {
return <AppNavigator screenProps={{
//pass any object you need for ex
testing:true
}}/>
}
}
,但我添加了另一个信息,用于使用以下道具:
对于访问这些道具,您可以简单地使用:
console.log(this.props.screenProps.testing)
在createSwtichNavigator
中定义的任何屏幕上,
例如,您的代码:
Loading, Auth, and Main
应该得到道具
class Loading extends React.Component {
componentWillMount(){
console.log(this.props.screenProps.testing)
}
render() {
return (<View/>)
}
}
注意: 我使用
测试了代码React Navigation V2