我是新来reactnativereactnative通导航到子视图
我使用FlatList
与app.js
,并使用StackNavigator
对于这样的场景之间导航:
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Content',
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<FlatList
data = {myData}
renderItem={({item}) =>
<Text style={styles.someStyle}
onPress={()=>
navigate("Detail",
{item: item});}>
{item.text}</Text>
}
/>
</View>
);
}
}
上面的代码将很好地工作:当我按下一个列出的项目,将加载另一个场景以显示项目内容的详细信息。
但因为我想为所列项目复杂的造型,我认为这将是方便的,我在这样的一个子视图定义的列表项目:
class MyHomeListItem extends React.PureComponent {
render(){
return (
<View style={styles.item}>
<Image
source={require('./assets/book.png')}
style={styles.listImageIcon}
/>
// as you can see the following `onPress` was intended for
// the same poperse which should load another secene to show the detail.
<Text onPress={()=>
{this.props.ref("Detail",
{item: this.props.item});}}
style={styles.listItemHomeText}>
{this.props.chapterName}: {this.props.chapterTitle}
</Text>
</View>
);
}
}
而且我想通过在数据FlatList
作为一个属性,如:
<FlatList
data = {myData}
renderItem={({item}) => <MyHomeListItem chapterName={item.chapterName} chapterTitle={item.chapterTitle} ref={navigate} /> }
/>
如果我没有在navigate
功能传递给子视图,那么就会出问题,我认为是好的,所以我将它传递通过ref
到目前为止是的参考navigate
功能(我想)或只是通过ref={this}
和在子视图中使用this.props.ref.navigate(...)
,但要么不工作。
它会显示:
所以_this3.props.ref is not a function....'_this3.props.ref' is undefined
我怎样才能做到这一点?
Furthur多,其实我是想整子视图听onPress
但我没有找到<View>
子视图中的属性值是什么? – error404
'_this3.props.ref不是一个函数...._ this3.props.ref是不确定的' – armnotstrong
我的意思是你可以在render方法中使用console.log(this.props)共享完整的日志。在return语句之前放置该行并共享日志。 – error404