随处使用导航方法

时间:2017-11-20 02:24:20

标签: reactjs react-native react-navigation

我有一个未在StackNavigator中分配的组件。不过,我仍然希望使用navigate中的react-navigation方法。这可能吗?

例如:

manage.js

export const mainStack = StackNavigator({
  Home: { screen: HomeScreen},
  Content: { screen: ContentScreen },
});

OtherContent.js

class OtherContent extends React.Component {
    constructor(props) {
      super(props);
    }

    _MoveToScreen = () =>{
        //try to open ContentScreen from here
        const { navigate } = this.props.navigation;
        navigate('ContentScreen');
    }

}

我想在navigate组件中使用OtherContent。如何将navigation道具传递给组件。

2 个答案:

答案 0 :(得分:0)

使用redux并将其置于状态,然后你可以从那里调用它我认为

答案 1 :(得分:0)

您可以将navigation作为道具从父级传递,或者使用withNavigation更高级别的组件react-navigation

示例1:

<OtherContent navigation={this.props.navigation} />

示例2:

import React from 'react';
import { withNavigation } from 'react-navigation';

class OtherContent extends React.Component {
    constructor(props) {
      super(props);
    }

    _MoveToScreen = () =>{
        //try to open ContentScreen from here
        const { navigate } = this.props.navigation;
        navigate('ContentScreen');
    }
    // ...
}

const OtherContentWithNavigation = withNavigation(OtherContent);