React Native:可能未处理的承诺拒绝:找不到变量:名称 [英] React Native: Possible UNhandled Promise Rejection: Can't find variable: name

查看:56
本文介绍了React Native:可能未处理的承诺拒绝:找不到变量:名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试制作一个需要 3 个日期的应用程序时,我偶然发现了一个问题:

While trying to make an app that would ask for 3 dates, i've stumbled on a problem :

我有这个:

onDateChange = (state) => (event, value) => {
    this.setState({
        [state]: name
    });
}

被调用者:

<DatePicker
    dateMes={this.state.dateMes}
    mode="date"
    placeholder="select date"
    format="DD-MM-YYYY"
    minDate="01-01-1950"
    maxDate="01-01-2050"
    androidMode="spinner"
    showIcon={false}
    onDateChange={this.onDateChange('dateMes')}
/>

加载后,我的 3 个日期选择器被占位符填满.当我打开它们并选择一个日期时,什么都不会改变(好像我从未选择过一个日期)

When loaded, my 3 datepickers are juste filled with the placeholder. When i open them and select a date, nothing will change (as if i never selected a date)

我相信它没有找到如何使 onDateChange 工作,但我不知道如何使其工作

I believe it's not finding how to make the onDateChange work, but I don't know how to make it work

感谢您的帮助!

感谢 Chris G,我找到了解决方案.我的功能现在看起来像

Thanks to Chris G, i got a solution. My function now looks like

onDateChange = (state) => (event, name) => {
    this.setState({
        [state]: name
    });
}

错误消失了,但日期选择器并未更改日期.

The error disappeared, but the date are not changed with the datepicker.

推荐答案

让我给出一些看法, OnDateChange 道具接收一个作为回调的函数,你正在分配它很好,但是当回调被调用时引用 'this' 已经改变,它无法找到受类限制的变量

Let me give this some perspective , OnDateChange prop receives a function which act as a callback you are assigning it fine , but when the callback is invoked reference of 'this' has changed and it couldnt find the variables which are bounded by the class

现在在 react-native 示例中,他们使用了 Arrow function ,它在词法上限制了 'this' 的范围,这解释了为什么您可以找到类变量,因为 'this' 仍然指的是组件.参考:箭头函数

Now in react-native example they have used Arrow function , which lexically bound the scope of 'this' which explains why you could find the class variables because 'this' still refers to component. Reference : Arrow Functions

这篇关于React Native:可能未处理的承诺拒绝:找不到变量:名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆