简体   繁体   English

在异步函数中调用函数(呈现组件)

[英]Call Function(that render component) in async Function

Good day everyone. 今天是个好日子。

I have problem with this piece of code: 我对这段代码有疑问:

It's 2 function: 这是2个功能:

1. renderModal() - it's responsible for rendering ModalSuccess at the moment where data sucesfully will be added to databbase (to inform user about correctly fill form. 1. renderModal() -它负责在将数据成功添加到数据库时渲染ModalSuccess(以通知用户正确填写表单。)。

Component ModalSuccess when call it's render modal. 组件ModalSuccess在调用时为渲染模态。

2. submitToServer - it's sending all data from redux-form to API. 2. SubmitToServer-它正在将所有数据从redux-form发送到API。

In end of try , i trying call function renderModal. try结束时,我尝试调用函数renderModal。

How can i make it correctly? 我如何正确制作?

function renderModal() {
  return (
    <div>
      <ModalSuccess/>
    </div>
  );
}

//async function send to server
export async function submitToServer(values) {

  //FUND
   try {
    let response = await fetch('endpoint', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        ...authHeader()
      },
      body: JSON.stringify(values),

    });

    let responseJson = await response.json();
    return responseJson;

    renderModal();

  } catch (error) {
    console.error(error);
  }

I call submitTo server in 2 places: 我在2个地方调用SubmitTo服务器:

1. 1。

export var submit =(values) =>{

          let isError =false;

          if (isError) {
            //  throw new SumissionError(error);
          } else{
            return submitToServer(values)
              .then(data =>{
                  if (data.errors)  {
                    console.log(data.errors);
                      throw new SubmissionError(data.errors);
                  } else{
                      console.log(values)
                      console.log('server added data to database');
                  }
              });
          }
}

2. 2。

<form onSubmit={handleSubmit(submitToServer)}>

I think you can restructure your code a bit better. 我认为您可以更好地重组代码。 Instead of returning the modal you can just mount the modal once and control its visibility leveraging the state. 无需返回模态,您只需安装模态一次并利用状态控制其可见性即可。

Take a look at how I think your component should be structured. 看一下我认为您的组件应该如何组织。

class Comp extends React.Component {
  state = {
    isOpen: false
  };
  submitToServer = async values => {
    try {
      let response = await fetch("endpoint", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          ...authHeader()
        },
        body: JSON.stringify(values)
      });

      let responseJson = await response.json();
      this.setState({ isOpen: true });
      return responseJson;
    } catch (error) {
      console.error(error);
    }
  };
  render() {
    /* your component */
    <ModalSuccess isOpen />;
  }
}

As it stands your renderModal() invocation will never register since you are returning once the response it has been returned. 就目前而言,您的renderModal()调用将永远不会注册,因为一旦返回响应,您将返回它。

What you'd need to is something like this: 您需要的是这样的东西:

let responseJson = await response.json();
if (responseJson) {
  renderModal();
}

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

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