首页 > 解决方案 > 反应本机 this.props.navigation 在主应用程序中未定义

问题描述

我试图在我的主要反应原生应用程序中使用“this.props.navigation”,但它是未定义的。在 TabNavigator 和 ScreenNavigator 附带的我的其他组件中,它可以工作。

我的 App.js

const MyTabView = TabNavigator(
  {     
    Projects: { screen: Projects },     
    Explorer: { screen: Explorer },
    Profil: { screen: Profil }, ... 
  }        
);



const StackView = StackNavigator(
  {
    global: {
      screen: MyTabView,
      headerMode: "none",
      header: null,
      navigationOptions: {
        header: null
      }
    },

    TermsOfUse: { screen: TermsOfUse },
    LegalNotice: { screen: LegalNotice },
    Options: { screen: Options }, ...       
  } 
);

export default class App extends React.PureComponent {
  constructor (props) {
    super(props); 
    console.log(this.props.navigation); // <----------  UNDEFINED
  }


  render() {
    return (
      <Provider store={store}>
        <PersistGate
          loading={<ActivityIndicator />}
          persistor={persistor}>

          <View style={styles.fullScreen}>
            <StackView />

            <Tutorial navigation={this.props.navigation} />  // <----------  this.props.navigation  is  UNDEFINED
          </View>
        </PersistGate>
      </Provider>
    );
  }
}

标签: react-native

解决方案


navigationprop 仅适用于导航堆栈中的组件或使用withNavigationHOC的子组件。


推荐阅读