React-Native: it is not possible to access state values ​​or in any way declared from renderRow

I have a list, whenever a user clicks on an element, I want to do something using onPress (touchable highlight).

I tried to define a function and then call them in onPress, but they did not work.

I first defined a function like this:

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);
    }

Then:

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>
    );
  }
}

But this did not work, error:

TypeError: Cannot read property '_dosome' of null(…)

I tried calling it onPress as follows:

onPress={ this._dosome.bind(this) }

did not help

onPress={ ()=> {this._dosome.bind(this)}}

did not help

onPress={ console.log(this.state.loaded)}

even I can’t even access the details defined in the constructor. he does not know what "this" is!

I tried to define the function as follows:

var _dosome = (dd) => {
      console.log(dd);
    };

I tried to define _dosome before the render, after the render, inside the render, inside renderRow and after renderRow. all the same results.

, rnplay, stackoverflow, , . /? . 2 . Advance!

+4
1

  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderRow}
    style={styles.listView}
  />

To

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

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

?

( bind), renderRow , .
renderRow={this.renderRow}, ( ).

+8

All Articles