react-native - React native - ListView行中的绑定(bind)事件

标签 react-native

我无法从 ListView 行下的基于类的定义中找到触发事件的正确方法。这是我到目前为止所做的

class SampleApp extends React.Component {

  constructor(props){
    super(props)
    this.state = {
        dataSource: ds.cloneWithRows(this._partners()),
    }

    // this._click = this._click.bind(this);
  }

 _click() {
    console.log('clicked');
  }

  _renderRow(rowData) {
    return (<TouchableHighlight onPress={() => {this._click();}}>
        <Text style={{ fontSize:18 }}>{rowData}</Text>
      </TouchableHighlight>);
  }

  render() {
    console.log('Partners: ', this._partners() )
    return (
      <View style={styles.container}>
        <ListView
         dataSource={this.state.dataSource}
         renderRow={ this._renderRow } />
      </View>
    );
  }
}

this 里面的 onPress 不是 react 组件。我该如何解决?这是 react Playground 链接 https://rnplay.org/apps/Xd-l3w

最佳答案

试试这个。

constructor(props){
  super(props)
  this.state = {
    dataSource: ds.cloneWithRows(this._partners()),
  }

  this._renderRow = this._renderRow.bind(this);
}

_renderRow() 不包含范围。重新绑定(bind)它可以解决问题。

onPress={this._click} 也可以工作,而不是将 this._click 放在另一个函数中。

这是一个 fork of your code .

关于react-native - React native - ListView行中的绑定(bind)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685618/

相关文章:

react-native - 如何在 MapView 中偏移中心点

https - React-native 0.40+ 获取自签名证书

android - 31.0.0 不是有效的 SDK 版本。选项是 26.0.0 未版本化

javascript - 如何将从 api 获取的一些单词而不是 'true' 或 'false' 值渲染到 FlatList 中?

reactjs - React-native 与 React 的兼容性

javascript - React Native - 良好实践 : SegmentedControlIOS with ListView

react-native - react native 切换样式

javascript - 如何在 expo 应用程序中处理共享 Intent (发送图像)?

android - WebStorm react-native 调试不工作

javascript - useState 和 useEffect 有什么区别?