简体   繁体   English

当在屏幕上显示来自 React 的 useState 钩子的值并且除非在操作下才重新渲染时,使用哪种策略?

[英]Which strategy to use when displaying a value on the screen that comes from a useState hook of React and not re render unless under an action?

I have a report generated by three parameters.我有一个由三个参数生成的报告。 An user and 2 range date fields.一个用户和 2 个范围日期字段。

eg:例如: 在此处输入图像描述

When all fields are filled, the report can be generated by Gerar button.填写完所有字段后,可以通过Gerar按钮生成报告。

eg when generated:例如,当生成时: 在此处输入图像描述

The problem i don't know how to handle happens now.我不知道如何处理的问题现在发生了。 When a value of any field is changed, report renders again.当任何字段的值发生更改时,报表会再次呈现。 As well the values of Motorista and Período fields of header report, since those values come from the parameters.以及 header 报告的MotoristaPeríodo字段的值,因为这些值来自参数。

Here is my code:这是我的代码:

useState hooks of parameters: useState参数挂钩:

const [selectedDriver, setSelectedDriver] = useState({});
const [startDate, setStartDate] = useState(new Date());
const [endDate, setEndDate] = useState(new Date());

useState that indicates when to show report or not: useState指示何时显示报告或不显示:

const [renderPDF, setRenderPDF] = useState(false);

Gerar button : Gerar button

          <Button
            type="submit"
            color="success"
            onClick={() => {
              setRenderPDF(false);
              handleEventsFromMatrix() //function that call an endpoint that brings data to the report
            }}
          >
            Gerar
          </Button>

handleEventsFromMatrix() , function that call an endpoint that brings data to the report: handleEventsFromMatrix() , function 调用将数据带入报告的端点:

async function handleEventsFromMatrix() {
    try {
      //rest of logic

      const response =
        await api.get(`endpoint`);

      if (response.data.generatedEvents.length > 0) {
        setRenderPDF(true);
      }
    } catch (err) {
      console.log(err);
    }
  }

Here is the validation rule to show Informe o motorista eo período para o qual você deseja gerar relatório de diários de bordo message or to bring report with data.这是显示Informe o motorista eo período para o qual você deseja gerar relatório de diários de bordo消息或带数据报告的验证规则。 renderPDF hook starts as false to bring the message and when request is succefully completed, turn it to true, bringing the report with data. renderPDF钩子以 false 开始以带来消息,当请求成功完成时,将其变为 true,带来带有数据的报告。

Message:信息:

{!renderPDF &&
  //rest of logic

  <div>
    <h1>Informe o motorista e o período para o qual você deseja gerar relatório de diários de bordo</h1>
  </div>
}

Show report with data:显示带有数据的报告:

{renderPDF &&
  <PDFViewer style={styles.page}>
  
  //rest of logic
}

What am i missing here?我在这里想念什么?

Can you try it after changing it like this?改成这样后可以试试吗? If not, please provide a sandbox to run the entire code.如果没有,请提供一个沙箱来运行整个代码。

      <Button
            type="submit"
            color="success"
            onClick={() => {
              //setRenderPDF(false); remove
              handleEventsFromMatrix() //function that call an endpoint that brings data to the report
            }}
          >
            Gerar
     </Button>
async function handleEventsFromMatrix() {
    try {
      //rest of logic

      const response =
        await api.get(`endpoint`);

        setRenderPDF(response.data.generatedEvents.length > 0);
      
    } catch (err) {
      console.log(err);
    }
  }
{renderPDF ?
  (
  <PDFViewer style={styles.page}>
    //rest of logic
  )
  :
  (
    <div>
    <h1>Informe o motorista e o período para o qual você deseja gerar 
         relatório de diários de bordo</h1>
     </div>
  )
  
  
  
}

With @Changhoon Lee and @Aidan Hakimian help, i was able to debug my code to handle what was missing.在@Changhoon Lee 和@Aidan Hakimian 的帮助下,我能够调试我的代码来处理缺失的内容。

As used useState hooks, every change in it re rendered the component (actually behavior).与使用useState挂钩一样,它的每一次更改都会重新呈现组件(实际上是行为)。 To avoid this, i had to use useRef .为了避免这种情况,我不得不使用useRef useRef save last value without re render components! useRef保存最后一个值而不重新渲染组件!

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

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