React-Native:无法访问状态值或从renderRow声明的任何方法

Ata*_*adi 5 javascript uinavigationcontroller ios reactjs react-native

我有一个listview,每当用户点击一个项目时,我想用onPress做一些事情(可触摸的高亮显示).

我尝试定义函数,然后在onPress中调用它们,但它们没有工作.

首先我定义了一个这样的函数:

constructor(props) {
        super(props);
        this.state = {
          dataSource: new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
          }),
          loaded: false,
        };
        this._dosome = this._dosome.bind(this);
    }

    _dosome(dd){
      console.log(dd);
    }
Run Code Online (Sandbox Code Playgroud)

然后 :

render(){
  if (!this.state.loaded) {
      return this.renderLoadingView();
    }

    return (
      <View style={{
        flex: 1
      }}>
      <ListView
        dataSource={this.state.dataSource}
        renderRow={this.renderRow}
        style={styles.listView}
      />
      </View>
    );
  }

  renderRow(data) {

    var header = (
      <View>
          <View style={styles.rowContainer}>
            <View  style={styles.textContainer}>
              <Text style={styles.title}>{data.nid}</Text>
              <Text style={styles.description} numberOfLines={0}>{data.title}</Text>
            </View>
          </View>
          <View style={styles.separator}></View>
    </View>
    );
///////////
    var cid = [];
    var content = [];
    for(let x=0; x < Object.keys(data.course).length; x++){
      cid[x] = data.course[x].course_id;
      content.push(
        <TouchableHighlight
        underlayColor='#e3e0d7'
        key={x}
        onPress={ this._dosome } //// **** PROBLEM HERE ****
        style={styles.child}
        >
        <Text style={styles.child}>
        {data.course[x].title}
        </Text>
        </TouchableHighlight>
      );
    }
    console.log(cid);
    var clist = (
      <View style={styles.rowContainer}>
      {content}
      </View>
    );
////////////
    return (
      <Accordion
        header={header}
        content={clist}
        easing="easeOutCubic"
      />
    );
  }


  renderLoadingView() {
    return (
      <View style={styles.loading}>
        <Text style={styles.loading}>
          Loading Courses, please wait...
        </Text>
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

但它没有用,错误:

TypeError: Cannot read property '_dosome' of null(…)
Run Code Online (Sandbox Code Playgroud)

我尝试像这样调用onPress:

onPress={ this._dosome.bind(this) }
Run Code Online (Sandbox Code Playgroud)

没有帮助

onPress={ ()=> {this._dosome.bind(this)}}
Run Code Online (Sandbox Code Playgroud)

没有帮助

onPress={ console.log(this.state.loaded)}
Run Code Online (Sandbox Code Playgroud)

即使我甚至无法访问构造函数中定义的道具.它不知道什么是"这个"!

我尝试用其他方式定义函数:

var _dosome = (dd) => {
      console.log(dd);
    };
Run Code Online (Sandbox Code Playgroud)

我尝试在渲染之前,渲染之后,渲染内部,renderRow之内以及renderRow之后定义_dosome.所有相同的结果.

我确实检查了许多教程,rnplay上的大量示例应用程序,stackoverflow中的大量线程,很多示例,但它们都没有为我工作.任何解决方案/想法?任何帮助都非常感谢.我现在正在努力解决这个问题2天.提前致谢!

Dav*_*uan 10

如何解决你的问题

更改

  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderRow}
    style={styles.listView}
  />
Run Code Online (Sandbox Code Playgroud)

  <ListView
    dataSource={this.state.dataSource}
    renderRow={data => this.renderRow(data)}
    style={styles.listView}
  />
Run Code Online (Sandbox Code Playgroud)

要么

  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderRow.bind(this)}
    style={styles.listView}
  />
Run Code Online (Sandbox Code Playgroud)

为什么?

正如您已经知道的那样(因为您试图通过使用箭头函数来解决问题bind),要调用renderRow,调用者应该与组件本身处于相同的上下文中.
但是这种连接丢失了renderRow={this.renderRow},这创造了一个新的功能(新的上下文).