从 parent 导航到特定组件(React Native Navigation)

Navigate to a specific component from parent (React Native Navigation)

菜鸟关于 React Native Navigation 的问题。

这是我的App.js。当我启动它时,我看到 Component1.

import React, { Component } from 'react';

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

class App extends Component {

  constructor(props){
    super(props);
  }

  myFunction(){
    /*
      -------------------------------------------
      Navigate from Component1 to Component2 (???)
      -------------------------------------------
    */
  }

  render(){
    return(
          <Stack.Navigator>
              <Stack.Screen name="Component1" component={Component1} />
              <Stack.Screen name="Component2" component={Component2} />
          </Stack.Navigator>
    );
  }

}

export default App

从同一个 class,在 myFunction 中,我想导航到 Component2。怎么做?

我找到的所有指南都解释了如何在 Component1 中使用导航道具进行操作。我需要从 parent.

开始

你可以试试这个

this.props.navigation.navigate("Component2")