【问题标题】:How to save the result of a mysql request in ReactJs?如何在 ReactJs 中保存 mysql 请求的结果?
【发布时间】:2021-11-12 17:36:16
【问题描述】:

我在 ReactJs 中有一个代码,它只是一个 SELECT。该请求工作正常,但我想将其结果保存到一个变量(或类似的东西)中。我该怎么做?

    const data;

    async function selecionaSubsistemas() {
        return Axios.post("http://localhost:3001/api/selecionaSubsistemas", {
        }).then((response)=>{
        });
    }
  
    const getData = async() => {
        data = await selecionaSubsistemas();
    };
  
    console.log(data);

【问题讨论】:

    标签: mysql reactjs express


    【解决方案1】:

    您可以将其保存为state

    react 中的状态:控制组件行为的一组可观察属性的对象

    使用 React Hook,你应该这样做:

    import React, { useEffect, useState } from "react";
    
    const [data, setData] = useState([]);
    
      ...
    
          .then((response)=>{
              setData(response.data) //so you access to the data with `data` variable.
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多