相关疑难解决方法(0)

函数中该值为null(React-Native)

根据本地测试,'this'在行渲染函数中似乎为null.因此,这阻止了我在onPress道具上绑定本地函数.

我有这个渲染块:

render() {
    return (
        <ListView
            dataSource={this.state.dataSource}
            renderRow={this._renderRow}
            renderHeader={this._renderHeader} 
            style={styles.listView} />
    );
}
Run Code Online (Sandbox Code Playgroud)

和一个本地功能

_visitEntryDetail() {
    console.log('test');
}
Run Code Online (Sandbox Code Playgroud)

然后行渲染

_renderRow(something) {
    return (
        <TouchableHighlight
            style={[styles.textContainer, filestyle.container]} 
            onPress={this._visitEntryDetail.bind(this)} >
            <View>
                <Text style={filestyle.text1} >{something.detail}</Text>
                <Text style={filestyle.text2} >{something.size}, {something.timestamp}</Text>
            </View>
        </TouchableHighlight>
    );
}
Run Code Online (Sandbox Code Playgroud)

这回来了

message: null is not an object (evaluating 'this.$FileList_visitEntryDetail')"
Run Code Online (Sandbox Code Playgroud)

检查renderRow上的"this"在替换上面的代码时返回null:

_renderRow(file) {
    console.log(this);
    return (
        <TouchableHighlight
            style={[styles.textContainer, filestyle.filelistcontainer]} 
             >
Run Code Online (Sandbox Code Playgroud)

以下控制台输出:

RCTJSLog> null
Run Code Online (Sandbox Code Playgroud)

但是很好

render() {
    console.log('inside render. this value is below me');
    console.log(this);
    return (
        <ListView
Run Code Online (Sandbox Code Playgroud)

安慰

RCTJSLog> "inside …
Run Code Online (Sandbox Code Playgroud)

listview this react-native

20
推荐指数
1
解决办法
1万
查看次数

标签 统计

listview ×1

react-native ×1

this ×1