Sed*_*rei 26 react-native react-navigation
我使用DrawerNavigator,我有3页:Router page,mainScreen和photos page,
我maked头导航栏区域,我用这个<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>在mainScreen打开抽屉菜单,并使用提供的照片太过页面,菜单是确定在mainScreen,但是当我点击<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>的照片页面,我收到此错误:

我该如何解决这个问题?
我的照片页面:
import React from 'react';
import { Button, ScrollView, View, Text, StyleSheet, TouchableHighlight } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import Icon from 'react-native-vector-icons/FontAwesome'
const MyNavScreen = ({ navigation }) => (
<View>
<View style={styles.containerNavbar}>
<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Icon name="bars" size={30} color="#fff" />
</TouchableHighlight>
<Text style={styles.navbarTitle}>Photos</Text>
</View>
<ScrollView>
<View><Text>photo</Text></View>
<Button onPress={() => navigation.goBack(null)} title="Go back" />
</ScrollView>
</View>
);
const MyPhotosHomeScreen = ({ navigation }) => (
<MyNavScreen navigation={navigation} />
);
MyPhotosHomeScreen.navigationOptions = {
title: 'Photos',
drawerIcon: ({ tintColor }) => (
<MaterialIcons
name="photo"
size={24}
style={{ color: tintColor }}
/>
),
};
export default MyPhotosHomeScreen;
Run Code Online (Sandbox Code Playgroud)
主屏幕:
export default class MainScreen extends React.Component {
static navigationOptions = {
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
<MaterialIcons
name="home"
size={24}
style={{ color: tintColor }}
/>
)
};
render() {
return (
<View>
<View style={styles.containerNavbar}>
<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Icon name="bars" size={30} color="#fff" />
</TouchableHighlight>
<Text style={styles.navbarTitle}>mainScreen</Text>
</View>
<View>
<View style={styles.containerFooter}>
<Text style={styles.footerTitle}>Footer</Text>
</View>
</View>
</View>
)
}
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*eng 16
也许我忽略了一些东西,但它看起来像一个简单的Javascript错误.你在纯组件中破坏你的道具MyNavScreen:
const MyNavScreen = ({ navigation }) => (
Run Code Online (Sandbox Code Playgroud)
这意味着您无权访问this.props.你只需要访问破坏的道具navigation.因此,未定义错误的原因确实未定义:
<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
Run Code Online (Sandbox Code Playgroud)
如果你改为navigation直接使用它,它应该工作:
<TouchableHighlight onPress={() => navigation.navigate('DrawerOpen')}>
Run Code Online (Sandbox Code Playgroud)
在mainScreen,你很好,因为它不是一个带有析构参数的纯组件.所以,你还是要访问this.props在render().
如果这会导致你的麻烦,你应该重新进行破坏.
ber*_*gle 13
就我而言,当我绑定this到prop在构造函数中调用它的方法时,它就起作用了。
constructor(props){
super(props);
this.showDetails = this.showDetails.bind(this);// you should bind this to the method that call the props
}
showDetails(_id){
this.props.navigation.navigate('Details');
}
Run Code Online (Sandbox Code Playgroud)
showDetails = (_id) => {
this.props.navigation.navigate('Details');
}
Run Code Online (Sandbox Code Playgroud)
因为在使用表达式函数时,它将创建自己的作用域。
小智 12
<ChildComponent navigation={this.props.navigation}/>
Run Code Online (Sandbox Code Playgroud)
props.navigation.navigate("ScreenName")
Run Code Online (Sandbox Code Playgroud)
小智 8
如果在子元素中使用TouchableOpacity/Height,请将其传递给this.props.onPress:
<TouchableOpacity onPress={this.props.onPress}/>
Run Code Online (Sandbox Code Playgroud)
然后调用父组件中的onPress函数,如下所示:
<Parent onPress={this.Handlepress} />
Run Code Online (Sandbox Code Playgroud)
小智 7
尝试这个:
import { withNavigation } from 'react-navigation';
Run Code Online (Sandbox Code Playgroud)
withNavigation 在整个项目/应用中提供道具,您可以从任何地方访问导航道具。
和
onPress={() => this.props.navigation.navigate('DrawerOpen')}
Run Code Online (Sandbox Code Playgroud)
最后,
export default withNavigation(MyPhotosHomeScreen);
Run Code Online (Sandbox Code Playgroud)
看看这个https://reactnavigation.org/docs/en/connecting-navigation-prop.html
| 归档时间: |
|
| 查看次数: |
46428 次 |
| 最近记录: |