我正在尝试创建无状态组件来显示Flatlist,我正在努力进行导航 .
这是我的“主要”app.js,被剥离:
import React from 'react';
import { AppRegistry, FlatList, ActivityIndicator, Text, View, Image, StyleSheet } from 'react-native';
import { StackNavigator } from 'react-navigation'
import Strip from '../components/Strip'
export default class FetchData extends React.Component {
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
...
}
render(){
if(this.state.isLoading){
...
}
return (
<View>
<Strip props={this.state.dataSource.strips} />
</View>
)
}
}
这是组件:
import React from 'react';
import { FlatList, Text, View, Image, StyleSheet, TouchableOpacity } from 'react-native';
renderItem = ({item}) => (
<TouchableOpacity onPress={() => this.props.navigation.navigate('Details')} >
<View style={{width: 136}}>
...
<Text>some text</Text>
</View>
</TouchableOpacity>
);
const Strip = (props) => {
return Object.keys(props).map(function(key, i) {
return Object.keys(props[key]).map((strips, i) => {
var strip = props[key][strips];
return(
<View>
<Text>{strip.title}</Text>
<FlatList horizontal
data={strip.someobjects}
renderItem={({item}) => this.renderItem(item)}
/>
</View>
)
});
})
}
export default Strip;
列表显示但当然,当我触摸一个项目时,我得到'undefined不是一个对象(评估'_this.props.navigation')'错误 .
我知道我不能在无状态组件上使用this.props.navigation.navigate,但我只是不明白如何通过无状态组件中的平面列表传递导航道具 .
它必须是非常简单的东西,如使用导航('Details')并将const = this.props.navigation;某处 . 但是哪里?
2 回答
而不是来自
Strip
组件的navigate
,为Strip
提供onPress
处理程序并从那里导航 . 如果FetchData
是StackNavigator
的一部分,那么您可以轻松地从此组件中获取navigate
.考虑以下示例
在
Strip
组件中,您可以绑定onPress
处理程序希望这会有所帮助!
在父级中定义导航功能,并通过道具将其传递给子级 .
例如:
亲
儿童