【问题标题】:Call parent function from child component in React-Native ListView row从 React-Native ListView 行中的子组件调用父函数
【发布时间】:2016-09-07 12:21:15
【问题描述】:

我已经看到了这个问题的几个例子,但无法让任何一个工作,或者完全理解它是如何组合在一起的。我有一个名为ParentListView 的组件和另一个名为ChildCell 的组件(listView 中的一行)我希望ChildCell 中的onPress 调用ParentListView 中的函数。

class ChildCell extends Component {

  pressHandler() {
    this.props.onDonePress;
  }

  render() {
    return (
        <TouchableHighlight onPress={() => this.pressHandler()} >
          <Text>Child Cell Button</Text>
        </TouchableHighlight>
    );
  }

}

ParentListView:

class ParentListView extends Component {  

//...

  render() {

    return (
      <ListView
        dataSource={this.state.dataSource}
        style={styles.listView}
        renderRow={this.renderCell}
        renderSectionHeader={this.renderSectionHeader}
        />
    );
  }

  renderCell() {
    return (
      <ChildCell onDonePress={() => this.onDonePressList()} />
    )
  }

  onDonePressList() {
    console.log('Done pressed in list view')
  }

}

我认为这就是所有相关的代码。我可以让媒体注册希望ChildCell,但无法获得在ParentListView 中触发的方法。我错过了什么?

提前致谢!

更新 1:

ParentListView 中,如果我将传入的道具更改为:

<ChildCell onDonePress={this.onDonePressList.bind(this)} />

我在渲染单元格时在编译时收到Unhandled JS Exception: null is not an object (evaluating 'this.onDonePressList') 错误。

如果我这样直接把console.log放进去:

<ChildCell onDonePress={() => console.log('Done pressed in list view')} />

它可以很好地记录消息。

如果我像原来一样离开它:

<ChildCell onDonePress={() => this.onDonePressList()} />

它在 buttonPress 上崩溃,Unhandled JS Exception: null is not an object (evaluating '_this2.onDonePressList')

更新 2:

好的,我已经尝试像这样在构造函数中绑定方法:

class ParentListView extends Component {
  constructor(props) {
    super(props);
    this.onDonePressList = this.onDonePressList.bind(this);
    this.state = {...};
}

但它给了我这个错误:null is not an object (evaluating 'this.onDonePressList') 并且不会运行。

更新 3: Here is a link to a react native playground

【问题讨论】:

  • 您是否尝试过使用构造函数编写组件?根据反应文档:“我们建议您在构造函数中绑定事件处理程序,以便每个实例只绑定一次”facebook.github.io/react/docs/reusable-components.html
  • 我试过(见更新 2)但无济于事。 () =&gt; myFunction() 不帮我绑定吗?

标签: function listview react-native row components


【解决方案1】:

尝试像这样在pressHandler 中调用onDonePress

pressHandler() {
  this.props.onDonePress()
}

另外,将this 绑定到您的renderRowrenderSectionHeader

<ListView
    dataSource={this.state.dataSource}
    style={styles.listView}
    renderRow={this.renderCell.bind(this)}
    renderSectionHeader={this.renderSectionHeader.bind(this)}
    />

我已经使用上述函数设置了一个示例here

https://rnplay.org/apps/DcdAuw

【讨论】:

  • 当我这样调用时,我得到:Unhandled JS Exception: null is not an object (evaluating '_this2.onDonePressList')
  • 你的工作正常,哈哈。我试过&lt;ChildCell onDonePress={() =&gt; console.log('Done pressed in list view')} /&gt; 并且可行,但试图让父级使用this 调用它自己的函数似乎是个麻烦...
  • 我不确定您所拥有的和我设置的之间有什么区别,我是否遗漏了什么?我已经更新了两个组件以在示例中使用类,但功能是相同的。
  • 据我所知,您还没有将() 添加到this.props.onDonePress 的末尾(应该是this.props.onDonePress()
  • 就是这样!非常感谢。来自 Objective-C,this 的实现和使用一直是一个很难掌握的概念。
猜你喜欢
  • 2017-03-11
  • 2020-11-02
  • 2020-02-27
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2020-02-15
  • 2020-03-28
相关资源
最近更新 更多