javascript - React navigation withNavigation HOC 在轮播中不起作用

标签 javascript reactjs react-native

我正在使用 expo 制作一个 react native 应用程序,我正在使用 snap-in-carousel 库

我希望当有人点击轮播时它导航到这里是代码

    import React, { Component } from 'react';
import { withNavigation } from 'react-navigation';

export default class SliderEntry extends Component {

    static propTypes = {
        data: PropTypes.object.isRequired,
        even: PropTypes.bool,
        parallax: PropTypes.bool,
        parallaxProps: PropTypes.object
    };

    get image () {
        const { data: { illustration }, parallax, parallaxProps, even } = this.props;

        return parallax ? (
            <ParallaxImage
              source={{ uri: illustration }}
              containerStyle={[styles.imageContainer, even ? styles.imageContainerEven : {}]}
              style={styles.image}
              parallaxFactor={0.35}
              showSpinner={true}
              spinnerColor={even ? 'rgba(255, 255, 255, 0.4)' : 'rgba(0, 0, 0, 0.25)'}
              {...parallaxProps}
            />
        ) : (
            <Image
              source={{ uri: illustration }}
              style={styles.image}
            />
        );
    }

    render () {

        const { data: { title, subtitle}, even, navigation } = this.props;    

        const uppercaseTitle = title ? (
            <Text
              style={[styles.title, even ? styles.titleEven : {}]}
              numberOfLines={2}
            >
                { title.toUpperCase() }
            </Text>
        ) : false;

        return (
            <TouchableOpacity
              activeOpacity={1}
              style={styles.slideInnerContainer}
              onPress={() => navigation.push('ProfileScreen', {category: title })}  
              >
                <View style={styles.shadow} />
                <View style={[styles.imageContainer, even ? styles.imageContainerEven : {}]}>
                    { this.image }
                    <View style={[styles.radiusMask, even ? styles.radiusMaskEven : {}]} />
                </View>
                <View style={[styles.textContainer, even ? styles.textContainerEven : {}]}>
                    { uppercaseTitle }
                    <Text
                      style={[styles.subtitle, even ? styles.subtitleEven : {}]}
                      numberOfLines={2}
                    >
                        { subtitle }
                    </Text>
                </View>
            </TouchableOpacity>
        );
    }
}

我得到 undefined is not an object (evaluating 'navigation.push')

这里是 Github 上该项目的链接:https://github.com/Ov3rControl/Weddi enter image description here

最佳答案

您实际上并没有使用 withNavigation,您只是将其导入。您需要将组件类传递到 withNavigation HOC 中。

withNavigation 的工作方式是,您传入组件,withNavigation 将导航对象作为 prop 添加到组件中。

你没有这样做,因此 this.props.navigation 未定义。

请参阅下面修改后的代码,导出默认表达式已移至底部,并通过 withNavigation(SliderEntry) 传递。

阅读手册。 https://reactnavigation.org/docs/en/with-navigation.html

import React, { Component } from 'react';
import { withNavigation } from 'react-navigation';

class SliderEntry extends Component {

    static propTypes = {
        data: PropTypes.object.isRequired,
        even: PropTypes.bool,
        parallax: PropTypes.bool,
        parallaxProps: PropTypes.object
    };

    get image () {
        const { data: { illustration }, parallax, parallaxProps, even } = this.props;

        return parallax ? (
            <ParallaxImage
              source={{ uri: illustration }}
              containerStyle={[styles.imageContainer, even ? styles.imageContainerEven : {}]}
              style={styles.image}
              parallaxFactor={0.35}
              showSpinner={true}
              spinnerColor={even ? 'rgba(255, 255, 255, 0.4)' : 'rgba(0, 0, 0, 0.25)'}
              {...parallaxProps}
            />
        ) : (
            <Image
              source={{ uri: illustration }}
              style={styles.image}
            />
        );
    }

    render () {

        const { data: { title, subtitle}, even, navigation } = this.props;    

        const uppercaseTitle = title ? (
            <Text
              style={[styles.title, even ? styles.titleEven : {}]}
              numberOfLines={2}
            >
                { title.toUpperCase() }
            </Text>
        ) : false;

        return (
            <TouchableOpacity
              activeOpacity={1}
              style={styles.slideInnerContainer}
              onPress={() => navigation.push('ProfileScreen', {category: title })}  
              >
                <View style={styles.shadow} />
                <View style={[styles.imageContainer, even ? styles.imageContainerEven : {}]}>
                    { this.image }
                    <View style={[styles.radiusMask, even ? styles.radiusMaskEven : {}]} />
                </View>
                <View style={[styles.textContainer, even ? styles.textContainerEven : {}]}>
                    { uppercaseTitle }
                    <Text
                      style={[styles.subtitle, even ? styles.subtitleEven : {}]}
                      numberOfLines={2}
                    >
                        { subtitle }
                    </Text>
                </View>
            </TouchableOpacity>
        );
    }
}

// See the component is being wrapped with withNavigation.
export default withNavigation(SliderEntry);

关于javascript - React navigation withNavigation HOC 在轮播中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57625088/

相关文章:

javascript - 请求 GitHub API 时出现 401 Unauthorized 错误

javascript - 在 React Native 中导出方法/函数

react-native - 来自打包程序的 native react 中的条目文件错误

javascript - 必须通过单击 Javascript/ReactJs 中的按钮来发送电子邮件

reactjs - react native 日历

javascript - Facebook 应用程序 - Javascript 设置 Canvas 高度?

javascript - 未找到 AngularJS 页面。主页已注入(inject)索引模板,但未注入(inject)其他页面

javascript - Meteor.loginwithpassword 在 iOS 上不起作用

javascript - findthebest.com 使用哪个图表框架

javascript - 在 React Native 中使用 map 访问嵌套的 json