【问题标题】:cannot bind functions in react native无法在本机反应中绑定函数
【发布时间】:2016-05-10 17:07:13
【问题描述】:

我已经尝试了我所知道的所有可能的解决方案。我已经尝试了其他几种类似问题的解决方案,但我不明白这应该如何工作。我正在尝试从 ListView renderRow 方法中调用一个函数。我现在可以调用该函数,但我无法传入我需要的选定 rowData。这就是我得到的:

我需要选中的 rowData。这是代码

     constructor(props) {
        super(props);
        this.onSubmitTrip = this.onSubmitTrip.bind(this);
        this.onSelectTrip = this.onSelectTrip.bind(this);
        this.renderRow = this.renderRow.bind(this);

        this.state = {
            dataSource: ds,
            showSubmitTripBtn: false
        }
    }
    onSelectTrip(rowData) {
        selectedTrip = rowData;
    }

  renderRow(rowData) {
        return (
            <TouchableHighlight onPress={this.onSelectTrip}  underlayColor='#dddddd'>
                <View style={styles.row}>
                    <View style={{ paddingLeft: 5 }}>
                        <View>
                            <Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
                        </View>
                    </View>
                </View>
            </TouchableHighlight>
        )
    }

【问题讨论】:

  • “我现在可以调用该函数,但我无法传入我需要的选定 rowData。” 我看不到您在哪里调用该方法。传递数据就像this.renderRow(theData)一样简单。

标签: reactjs react-native ecmascript-6


【解决方案1】:

好吧,据我所知,当您按下 TouchableHighlight 时,您实际上并没有传递 onSelectTrip 您的 rowData。

// Change this
<TouchableHighlight
  onPress={this.onSelectTrip}
  underlayColor='#dddddd'
>

// To this
<TouchableHighlight
  onPress={() => this.onSelectTrip(rowData)}
  underlayColor='#dddddd'
>

此外,在查看您的屏幕截图时,我注意到您的 lodash _.each 调用中的 onSelectTrip 方法中的另一个潜在问题。如果你想在里面使用this.setState(),你需要传递_.each一个粗箭头作为回调。

// Change this
_.each(rowData.routeLocations, function(item) {

// To this
_.each(rowData.routeLocations, (item) => {

【讨论】:

    【解决方案2】:

    如何使用 ES6 lambda 代替显式绑定类构造函数中的所有方法来提供词法绑定。我认为这应该可以解决您的问题:

    constructor(props) {
        super(props);
        this.onSubmitTrip = this.onSubmitTrip.bind(this);
        this.renderRow = this.renderRow.bind(this);
    
        this.state = {
            dataSource: ds,
            showSubmitTripBtn: false
        }
    }
    
    onSelectTrip = (rowData) => {
        selectedTrip = rowData;
    }
    
    renderRow(rowData) {
        return (
            <TouchableHighlight onPress={this.onSelectTrip(rowData)}  underlayColor='#dddddd'>
                <View style={styles.row}>
                    <View style={{ paddingLeft: 5 }}>
                        <View>
                            <Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
                        </View>
                    </View>
                </View>
            </TouchableHighlight>
        )
    }
    

    【讨论】:

    • 它似乎正在触发页面加载功能。除了我的代码下面的一堆红线之外,我得到 TypeError: Cannot read property 'setState' of undefined(...) !!!
    • 以上代码是否完整?我没有看到任何setState
    • 不,我会用它来创建 plunker。不知道你需要什么。
    • 尝试使用 lambda 在 onSelectTrip 方法中更改 _.each 的匿名函数。
    • 它允许视图加载没有问题。但该功能没有被触发 onPress
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多