我是新來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