简体   繁体   English

如何在反应中将同步数据从函数发送到基于类的组件?

[英]How to send synchronous data from a functional to a class based component in react?

Hi I am learning asynchronous data handling in JavaScript and react.嗨,我正在学习 JavaScript 中的异步数据处理并做出反应。 I want to make the data provided by an asynchronous functional component available in the state of a class based component.我想让异步功能组件提供的数据在基于类的组件的状态下可用。

However I have the problem to actually access the received data in the state of my class based component.但是,我在基于类的组件的状态下实际访问接收到的数据时遇到了问题。

Here is my asynchronous functional component that returns a value:这是我的异步函数组件,它返回一个值:

export function validateAudioFile () {


        setTimeout(function(){ return "hallo" }, 1000);

  }

Here is my class based component that receives the value:这是我接收值的基于类的组件:

import {validateAudioFile} from './AudioFileValidation';



export class AudioUploadView extends Component {

constructor(props) {
    super(props);
    this.validationReady = this.validationReady.bind(this);



  this.state = {
    selectedFileValidated:null
  };

}

  validationReady= () => new Promise(resolve => {

    this.setState({selectedFileValidated: validateAudioFile ()}) 

    if (this.state.selectedFileValidated)

    resolve(this.state.selectedFileValidated)
})

  render() {

//validateAudioFile ()

this.validationReady()
  .then(function(v) { 
    console.log(v); // remains undefined 
  })

    return (
      <React.Fragment>

      </React.Fragment>
    );
  }
}

I would like to use the data for conditional rendering and other things, that's why I need it in my state.我想将数据用于条件渲染和其他事情,这就是我在我的状态下需要它的原因。

I am happy for any clarification.我很高兴得到任何澄清。

You have a list of problems here, but the main one is:您在这里列出了一系列问题,但主要问题是:

setState is an async function, so you need to use callback when actually state is changed. setState 是一个异步函数,因此您需要在实际状态更改时使用回调。

so your function should looks like:所以你的函数应该是这样的:

 validationReady = () => new Promise(resolve => {
    this.setState({selectedFileValidated: validateAudioFile ()}, () => {
       resolve(this.state.selectedFileValidated)
   ) 
})

As you validationReady relies on the previous value of the state.当您validationReady 依赖于状态的先前值。

Your code needs a lot of improvement.您的代码需要大量改进。 First of all, in order to make an async call you need a class component(or functional component with hooks),otherwise your app will not be able to detect changes when the data arrives(which is the whole point of state in react).首先,为了进行异步调用,您需要一个类组件(或带有钩子的功能组件),否则您的应用程序将无法在数据到达时检测到更改(这是反应中的整个状态点)。

Also i cannot think of a single usecase where you need a separate component just to call a function.我也想不出一个用例,你需要一个单独的组件来调用一个函数。 You might as well define that function in your 'AudioUploadView' component.您也可以在“AudioUploadView”组件中定义该函数。 If you have such a usecase then use hooks in your functional component.如果你有这样的用例,那么在你的功能组件中使用钩子。

Furthermore, your function call 'validateAudioFile()' is not going to return anything at all since the return statement is defined in the anonymous function inside setTimeout.此外,您的函数调用 'validateAudioFile()' 根本不会返回任何内容,因为 return 语句是在 setTimeout 内的匿名函数中定义的。

Also keep in mind the point made above by @Jlexyc.还要记住@Jlexyc 上面提出的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM