我试图在react-fine-uploader完成上传文件并触发回调后访问react组件的道具。但是,上传器在组件之外,我似乎无法从回调函数访问组件的道具。
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

答案 0 :(得分:0)
尝试将其放入您的组件中:
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
你也可以把它放在渲染中而不是构造函数中:
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 <>
}
}
答案 1 :(得分:0)
uploader.on('onAllComplete', (id, name, response) => {
//
})
所以const仍然在类之外,但我可以访问里面的回调。