前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >react 函数内部调用全局函数

react 函数内部调用全局函数

作者头像
wo.
发布2021-06-15 11:39:12
2.7K0
发布2021-06-15 11:39:12
举报
文章被收录于专栏:了不得的专栏

错误信息 react TypeError: Cannot read property 'up' of undefined at 错误信息 react Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined

解决方法

您的ajax请求中的回调函数未绑定。当您将一个函数传递给另一个函数(作为回调)时,“ this”将是对它最终被调用时所处上下文的引用,而不是您编写它时所处的上下文。如果使用箭头功能,它将保留编写时的上下文。 your callback function from the ajax request is not bound. When you pass a function to another function (as a callback), "this" will be a reference to the context it is in when it is finally called, as opposed to what it is when you wrote it. If you use an arrow function, it will keep the context it had when you wrote it.

代码语言:javascript
复制
.then(function(res){
        this.setState({intentOptions: res.data});
    });

如果将此功能设置为箭头功能,则应解决该问题。 If you make this function an arrow function, the issue should be resolved.

代码语言:javascript
复制
.then((res) => this.setState({intentOptions: res.data}));
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档