[英]How do I access my react component props from a fineuploader const callback outside the class?
I'm trying to access a react component's props after react-fine-uploader finishes uploading files and fires a callback. 我正在尝试在react-fine-uploader完成文件上传并触发回调后访问react组件的props。 However, the uploader is outside the component and I can't seem to access the props of the component from the callback function.
但是,上载器不在组件之外,我似乎无法从回调函数访问组件的属性。
import React from 'react'; // other imports const uploader = new FineUploaderTraditional({ options: { request: { endpoint: '/upload', }, callbacks: { onAllComplete: function(id, fileName, response) { console.log("Fineuploader: onAllComplete") // trying to access component props here } } } }) class myReactComponent extends React.Component { } export default myReactComponent
Try putting it inside your component: 尝试将其放入组件中:
import React from 'react';
// other imports
class myReactComponent extends React.Component {
constructor(props) {
super(props);
const uploader = new FineUploaderTraditional({
options: {
request: {
endpoint: '/upload',
},
callbacks: {
onAllComplete: function(id, fileName, response) {
console.log("Fineuploader: onAllComplete")
// you should be able to access props here
}
}
}
})
}
}
export default myReactComponent
You could also put this in render instead of the constructor: 您也可以将其放在render中,而不是在构造函数中:
class myReactComponent extends React.Component {
render() {
const uploader = new FineUploaderTraditional({
options: {
request: {
endpoint: '/upload',
},
callbacks: {
onAllComplete: function(id, fileName, response) {
console.log("Fineuploader: onAllComplete")
// you should be able to access this.props here
}
}
}
})
....
return <>
}
}
after doing some more digging I found I can regester a callback handler inside the component using the following: 经过更多的挖掘之后,我发现我可以使用以下命令重新注册组件内部的回调处理程序:
uploader.on('onAllComplete', (id, name, response) => { // })
So the const is still outside the class, but I can access the callback inside. 所以const仍然在类之外,但是我可以在内部访问回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.