React 导航 withNavigation HOC 在轮播中不起作用
React navigation withNavigation HOC not working in carousel
我正在使用 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 项目的 link:https://github.com/Ov3rControl/Weddi
您实际上并没有使用 withNavigation,您只是在导入它。您需要将组件 class 传递到 withNavigation HOC。
withNavigation 的工作方式是,你传入你的组件,withNavigation 将导航对象作为 prop 添加到你的组件。
你没有这样做,因此 this.props.navigation 未定义。
查看下面修改后的代码,导出默认表达式已移至底部,并通过 Navigation(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);
我正在使用 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 项目的 link:https://github.com/Ov3rControl/Weddi
您实际上并没有使用 withNavigation,您只是在导入它。您需要将组件 class 传递到 withNavigation HOC。
withNavigation 的工作方式是,你传入你的组件,withNavigation 将导航对象作为 prop 添加到你的组件。
你没有这样做,因此 this.props.navigation 未定义。
查看下面修改后的代码,导出默认表达式已移至底部,并通过 Navigation(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);