我有一个未在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
道具传递给组件。
答案 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);