如何在listview中的子组件中添加事件?是否可以在listview中添加事件?

时间:2016-08-20 06:57:04

标签: react-native

'在我的列表视图中有两个组成部分:第一,第二     在第二个中有map函数返回第三个(数组元素)     我怎样才能通过第一,第二,第三和第一次点击事件内。征求意见和评论帖子中的子评论。在bellow updateComments不起作用。有一件事,WallUpdates也是儿童组件。'

class WallUpdates extends Component {
  constructor(props) {
    super(props);
    this.updateComments = this.updateComments.bind(this);
  }
  updateComments(){
    alert("updatecommentPost");
  }
  render() {
    return(
      <View>
      <Text>
      {this.props.allPost}
      </Text>
      <ListView
          dataSource={this.props.dataSource} renderRow={this.renderMovie} style={styles.listView}
        />
      </View>
    );
  }

  renderMovie(list) {
    var commentNodes = list.comment.map((item, i) => {
      console.log(item);
      console.log(item.commentcontent);
      return (<Comments key={item.cid} comment={item.commentcontent} />);
    });
    //

    return (
      <View style={styles.container} >
        <Text style={styles.year}>{list.postcontent + '\n\n'}</Text>
        <TouchableOpacity onPress={this.updateComments}>
          <View style={styles.buttonsmall}>
            <Text style={styles.buttonsmallText}>comments!</Text>
          </View>
        </TouchableOpacity>
        {commentNodes}
      </View>
    );
  }

}

1 个答案:

答案 0 :(得分:0)

我已经改变了 - renderMovie(list)到renderMovie =(list)=&gt;并将onPress = {this.updateComments}更改为onPress = {()=&gt; {this.updateMe()}}&amp;这对我有用。