[英]How to access global scope from cloudinary react widget
So I am having a trouble at figuring it out on how to access global scope from cloudinary react widget function, what i want to is to set state on response. 所以我很难弄清楚如何从cloudinary react小部件函数访问全局范围,我想要的是在响应时设置状态。
Function 功能
uploadWidget() {
cloudinary.openUploadWidget({ cloud_name: 'descl4zie', upload_preset: 'g3d6k0nl', tags:['xmas']},
function(error, result) {
console.log(result);
this.setState({photo: secure_url});
});
}
Render 给予
render() {
return (
<button onClick={this.uploadWidget.bind(this)} className="upload-button">
Add Image
</button>
)
}
Currently getting this error: Cannot read property 'setState' of undefined 当前出现此错误:无法读取未定义的属性“ setState”
The callback function given to openUploadWidget
is not bound to this
, so this
will not be what you expect inside of it. 给
openUploadWidget
提供的回调函数未绑定到this
,因此this
不是您期望的。
You could eg use an arrow function instead to use the enclosing lexical scope which has the this
value you want: 例如,您可以改用箭头函数来使用具有所需
this
值的封闭词法范围:
uploadWidget() {
cloudinary.openUploadWidget(
{ cloud_name: "descl4zie", upload_preset: "g3d6k0nl", tags: ["xmas"] },
(error, result) => {
console.log(result);
this.setState({ photo: result });
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.