React native 导航器中导航道具的问题
我在用这个React native 导航器中导航道具的问题,react-native,navigation,react-native-android,tabnavigator,React Native,Navigation,React Native Android,Tabnavigator,我在用这个 <View style={{flex:1}}> {this.state.screenSwitch ? ( <Screen1/> ) : <Screen2/>} </View> {this.state.screenSwitch( ) : } 作为回报。当我使用它在底部选项卡导航器的单
<View style={{flex:1}}>
{this.state.screenSwitch ? (
<Screen1/>
) : <Screen2/>}
</View>
{this.state.screenSwitch(
) : }
作为回报。当我使用它在底部选项卡导航器的单个选项卡中交换两个屏幕时,我使用
<TouchableOpacity onPress={()=> this.props.navigation.navigate('Screen3')}>
this.props.navigation.navigate('Screen3')}>
但是得到这样一个错误
undefined不是对象(计算“\u this2.props.navigation.navigate”)
那么问题出在哪里。您能否创建一个处理导航的函数,并按如下所示调用该函数
import {NavigationActions} from 'react-navigation';
constructor(props) {
super(props);
}
navigateToScreen = (route) => () => {
const navigateAction = NavigationActions.navigate({
routeName: route
});
this.props.navigation.dispatch(navigateAction);
}
render() {
...
<TouchableOpacity onPress={this.navigateToScreen('Screen3')} ></TouchableOpacity>
...
}
从'react navigation'导入{NavigationActions};
建造师(道具){
超级(道具);
}
导航屏幕=(路线)=>()=>{
const navigateAction=NavigationActions.navigate({
路由名称:路由
});
这个.props.navigation.dispatch(navigateAction);
}
render(){
...
...
}
这可能是由于此变量造成的。尝试创建一个名为self
的新变量,并将其放在类声明之前,然后执行以下操作:
... //imports below
import ....
//here put new variable self
let self;
// your class here
export default class YourClass extends Component {
.....
constructor() {
//your constructor fooes...
self = this;
}
.....
// and then use self instead of this in your code.
....
<TouchableOpacity onPress={()=> self.props.navigation.navigate('Screen3')}>
....
//进口低于
进口。。。。
//这里放入新变量self
让自己;
//你们班在这里吗
导出默认类YourClass扩展组件{
.....
构造函数(){
//你的构造函数是。。。
self=这个;
}
.....
//然后在代码中使用self而不是这个。
....
self.props.navigation.navigate('Screen3')}>
....
以上任何一项都不起作用。@Ajith在你的代码中我没有得到任何输出没有错误。请确保你的构造函数中包含了道具,我已经编辑了我的答案,它对我有效。我希望它也能对你起作用