0

CourseDetail.js当我单击 TouchableOpacity 时,如何导航到不同的屏幕 ( )。我正在使用它。但StackNavigator出现错误undefined is not an object evaluating this.props.navigation。我在下面粘贴我的代码。请帮忙。我知道有一个简单的错误,但无法弄清楚。

HomeScreen.js

import CourseDetail from './CourseDetail';
import { StackNavigator } from 'react-navigation';
const HomeScreen = ({course, navigation}) =>{
        const {name,featured_image,id} = course;
        const {navigate} = this.props.navigation;
        return(
            <TouchableOpacity onPress={() => navigate('CourseDetail', {id})}>
                <Card>
                    <CardSection>
                        <View style={styles.thumbnailContainerStyle}>
                             {course.term_id == '28' ? (<View></View>) : (
                             <Text style={styles.userStyle}>{name}
                             </Text> )}
                        </View>
                    </CardSection>
                </Card>
            </TouchableOpacity>
        );
    };
const ScheduledApp = StackNavigator({
    Home:{
        screen: HomeScreen
    },
    CourseDetail:{
        screen: CourseDetail
    }
});
export default HomeScreen;
4

3 回答 3

0

我使用 withNavigation 并解决了我的问题:

import { withNavigation } from 'react-navigation';
...
const MyHome = () => {
return (
    <TouchableOpacity onPress = {() => {props.navigation.navigate('SecondPage')}}
       <Text>go to other<Text>
    </TouchableOpacity>
);

};
export default withNavigation(MyHome)
于 2020-07-14T14:42:42.507 回答
0

HomeScreen 是一个功能组件,因此它没有绑定“this”关键字

于 2018-01-13T11:51:04.050 回答
0

你用

无状态功能组件

道具在参数中可用

import CourseDetail from './CourseDetail';
    import { StackNavigator } from 'react-navigation';
    const HomeScreen = ({course, navigation}) =>{
            const {name,featured_image,id} = course;
            const {navigate} = navigation; //No need of this.props
            return(
                <TouchableOpacity onPress={() => navigate('CourseDetail', {id})}>
                    <Card>
                        <CardSection>
                            <View style={styles.thumbnailContainerStyle}>
                                 {course.term_id == '28' ? (<View></View>) : (
                                 <Text style={styles.userStyle}>{name}
                                 </Text> )}
                            </View>
                        </CardSection>
                    </Card>
                </TouchableOpacity>
            );
        };
    const ScheduledApp = StackNavigator({
        Home:{
            screen: HomeScreen
        },
        CourseDetail:{
            screen: CourseDetail
        }
    });
    export default HomeScreen;
于 2018-01-14T12:47:46.520 回答