代码之家  ›  专栏  ›  技术社区  ›  Saeed Heidarizarei

通过React导航从任何组件访问导航道具

  •  1
  • Saeed Heidarizarei  · 技术社区  · 6 年前

    我有3个JS文件,2页( HomePage SecondPage (1) ComponentBlock 这增加了 主页 )
    我怎样才能从 构件块 第二页 ? 我的意思是,当我直接在主页上使用onpress按钮时,一切正常,我可以从主页导航到第二个,第二个导航到主页,但我不能从主页中添加的组件块导航到第二个。

    主页:

    import {createStackNavigator} from 'react-navigation';
    import Second from './8-SecondPage'
    import ComponentBlock from './8-Component-Block';
    
    class Home extends React.Component {
      render() {
        return (
          <ComponentBlock />
        );
      }
    }
    
    const App = createStackNavigator(
      {
        Home: {screen: Home},
        Second: {screen: Second},
        Third: {screen: ComponentBlock},
      },
    );
    
    export default App;
    

    SecondPage:

    export default class Second extends Component {
      render() {
        return (
          <View>
            <TouchableOpacity
              onPress={() => this.props.navigation.navigate('Home')}
            >
              <Text>Go to Home Page</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }
    

    ComponentBlock:

    export default class ComponentBlock extends Component {
      render() {
        return (
          <View>
            <Text>ComponentBlock</Text>
            <TouchableOpacity
              onPress={() => this.props.navigation.navigate('Second')}
            >
              <Text>Go to Second Page</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Mortada    6 年前

    在块组件文件中

    import { withNavigation} from 'react-navigation';
    export default withNavigation(BlockComponent);
    

    您现在可以使用

    this.props.navigation.navigate('SecondPage');