我在代码中使用道具来显示项目,但点击它后我无法导航到任何其他屏幕。我也定义为const { navigate } = this.props.navigation;
,但在屏幕截图中显示错误。
这是我的代码。此代码位于相同的Indprotype.js
文件中(不使用两个单独的.js文件)
class Withwish extends React.Component {
ProPressed(productid){
const { navigate } = this.props.navigation;
const params = { productid };
navigate('Product', params);
}
render() {
// const { navigate } = this.props.navigation;
return (
<View style={styles.word}>
<TouchableHighlight onPress={() => this.ProPressed(this.props.id)}>
<Image source={{uri: 'https://res.cloudinary.com/hu2lcbj2n/' + this.props.image}} />
</TouchableHighlight>
</View>
);
}
}
export default class Indprotype extends React.Component {
// const { navigate } = this.props.navigation;
render() {
const items = this.state.qwerty.data;
return (
<GridView
renderItem={item => (
<Withwish
image = {item.p1.image}
id = {item.p1.id}
/>
)}
/>
);
}
}
答案 0 :(得分:1)
试一试。
基本上我将navigation
道具从Indprotype
组件传递到Withwish
组件。
注意:我假设Indprotype
本身从路由器或类似设备接收navigation
道具。
class Withwish extends React.Component {
ProPressed(productid){
const { navigate } = this.props.navigation;
const params = { productid };
navigate('Product', params);
}
render() {
// const { navigate } = this.props.navigation;
return (
<View style={styles.word}>
<TouchableHighlight onPress={() => this.ProPressed(this.props.id)}>
<Image source={{uri: 'https://res.cloudinary.com/hu2lcbj2n/' + this.props.image}} />
</TouchableHighlight>
</View>
);
}
}
export default class Indprotype extends React.Component {
// const { navigate } = this.props.navigation;
render() {
const items = this.state.qwerty.data;
return (
<GridView
renderItem={item => (
<Withwish
navigation = {this.props.navigation}
image = {item.p1.image}
id = {item.p1.id}
/>
)}
/>
);
}
}