将道具传递给CreatesWitchnavigator破坏了CreateAppContainer



以下代码在我的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

测试了代码

相关内容

  • 没有找到相关文章

最新更新