简体   繁体   English

如何从类外部的fineuploader const回调访问我的react组件props?

[英]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.

相关问题 如何在类组件React之外访问props - How to access props outside of a class component React 如何从外部访问React Class方法? - How do I access a React Class method from outside? 如何从 function React 组件的 DOM 回调中访问上下文和组件 state? - How do I access the Context and Component state from within a DOM callback in a function React component? 如何将道具传递到反应组件? - How do I pass props to a react component? 如何将道具从一个组件发送到渲染之外的另一个组件? - How do I send props from one component to another component, outside the render? 我如何从道具访问我的反应组件中的历史记录? - how can i access the history in my react components from props? 如何将“ this”绑定到react类之外的函数,react类是其他组件的回调? - How to bind 'this' to functions outside react class which is a callback from other component? 我如何渲染作为带有附加道具的道具传入的反应组件? - How do i render a react component passed in as a props with additional props? 如何从回调中的React组件访问“this” - How to access “this” from a React component in a callback 使用Navigation.push(来自wix的本地导航v2)我没有在目标组件中获取道具。 我如何访问道具? - With Navigation.push (v2 of react native navigation from wix) I am not getting props in destination component. How do I access props?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM