javascript - react native : Passing DataSource into Method from Props in _renderRow

标签 javascript reactjs react-native

我正在尝试附加一个方法来接收传递到 ListView 的数据。具体来说,我正在利用 ListView React Native 组件及其 _renderRow 函数将数据源映射到不同的行。在数据源中,我想将其中一个数据元素作为参数传递给此方法:

class MainComponent extends Component {
    constructor(props) {
      super(props)
      var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
      this.state = { dataSource: ds.cloneWithRows([['foo1', 'bar1'], ['foo2','bar2']])};
      this._renderRow = this._renderRow.bind(this);
    }
    render() {
       return (
           <View>
             <ListView
               dataSource={this.state.dataSource} // [['foo1', 'bar1'], ['foo2', 'bar2']]
               renderRow={this._renderRow}
             />
           </View>
         );
    }
    _renderRow(rowData) {
      return (
         <SomeComponent onPress={_ => this.someMethod} data={rowData} /> 
         //Trying to call someMethod with argument 'bar1'.
         //I have also tried passing in the argument here with:
         // onPress={_ => this.someMethod({rowData[1]})}
       )
    }
    someMethod(data) {
      //do something with data
    }
}

class SomeComponent extends Component {
    render() {
      return (
         <TouchableHighlight
         underlayColor='#EFEFEF'
         onPress={this.props.onPress(this.props.data[1])}>
         //Is this the correct way to pass in 'bar1' into the method?
           <View>
             <Text>{this.props.data[0]}</Text>
           </View>
         </TouchableHighlight>
      )
   }
 }

那么,正确的做法是什么?我想将 DataSource 数据传递给 onPress 方法应该是相当常见的,但我在网上找不到任何东西。

谢谢!

最佳答案

在渲染行中的 onPress 中只需执行以下操作:

onPress={this.someMethod}

这应该可以解决问题。

关于javascript - react native : Passing DataSource into Method from Props in _renderRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447362/

相关文章:

javascript - 如何使用jquery更改按钮单击时聚焦的内容可编辑元素的类?

javascript - VueJS 2 v-for表单输入单选按钮检查属性不起作用

reactjs - 使用 Typescript 进行 Redux reducer 初始状态

react-native - 从商店 redux 获取状态值

reactjs - 首先添加一个子组件

reactjs - 使用 React-Native 和 Expo 进行 OCR

javascript - 图片定位用JS和Jquery

javascript - 在 Javascript Text Typer Effect 中需要帮助

reactjs - jsx 上的 eslint 缩进

javascript - 如何在 React 中添加图标 Fontawesome?