简体   繁体   中英

use navigate method anywhere

I have a component that not assign in StackNavigator . However I still want to use navigate method from react-navigation . Is that possible ?

For example:

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');
    }

}

I want to use navigate in OtherContent component. How can I pass the navigation props to the component.

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

You can pass navigation from parent as props or use withNavigation Higher Order Component by react-navigation

Example 1:

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

Example 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);

;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM