简体   繁体   English

React Native函数绑定参数

[英]React Native function bind parameters

I am trying to render a list which calls a function to render the rows, but how can I pass the paramater data to the function/handler? 我正在尝试渲染一个列表,该列表调用一个函数来渲染行,但是如何将参数数据传递给函数/处理程序呢? The data is in json style. 数据为json样式。

I tried many things but I always get an error back, what am I doing wrong? 我尝试了很多事情,但总是返回错误,我在做什么错?

This the code: 这段代码:

  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> ); } 

It seems you just need to save your response to the state. 看来您只需要保存对状态的响应即可。

this.setState({
  preLoad: true,
  dataJson: responseJson,
});

And do the binding correctly. 并正确进行绑定。

<List dataArray={this.state.dataJson}
   renderRow={(item) => this.renderRows.bind(this,item)}>
</List>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM