[英]Proper why to bind a function with parameters to child component in React Native
[英]React Native function bind parameters
我正在嘗試渲染一個列表,該列表調用一個函數來渲染行,但是如何將參數數據傳遞給函數/處理程序呢? 數據為json樣式。
我嘗試了很多事情,但總是返回錯誤,我在做什么錯?
這段代碼:
componentDidMount() { fetch('http://link/json.php', { method: 'GET' }) .then((response) => response.json()) .then((responseJson) => { dataJson = responseJson; this.setState({ preLoad: true }); }) .catch((error) => { }); } renderRows(item){ if(item.type == 2){ return( <ListItem> <Thumbnail square size={80} source={{ uri: 'item.image' }} /> <Body> <Text>{item.title}</Text> <Text note>{item.desc}</Text> </Body> </ListItem> ) } } renderMenu(){ if(this.state.preLoad){ return( <List dataArray={dataJson} renderRow={(item) => this.renderRows().bind(this,item) }> </List> ) }else{ return( <Body>{shimmerRows}</Body> ) } } render() { return ( <Container style={styles.container}> {this.renderMenu()} </Container> ); }
看來您只需要保存對狀態的響應即可。
this.setState({
preLoad: true,
dataJson: responseJson,
});
並正確進行綁定。
<List dataArray={this.state.dataJson}
renderRow={(item) => this.renderRows.bind(this,item)}>
</List>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.