如何通过服务器端的值在useEffect中设置状态

时间:2020-11-04 11:23:18

标签: reactjs

我正在开发一个应用程序,该应用程序从服务器端获取tofrom的值,为此我在useEffect之一中调度操作,并在获取值后将其传递给setState在useEffect内,但不起作用。我不知道为什么。

这是我的方式

const [timeSpan, setTimeSpan] = React.useState("Monthly");
  const [year, setYear] = React.useState(2020);
  const [tabValue, setTabValue] = React.useState(0);
  const [spanData, setSpanData] = React.useState([]);
  const [dataType, setDataType] = React.useState("aim");
  const [dataTo, setDataTo] = React.useState("");
  const [dataFrom, setDataFrom] = React.useState("");
     const {
        loading,
        duration,
        period,
        type,
        dispensingOverviewData,
        overviewDataLoading,
        defaultMonth,
      } = dispensingData;
      const { count } = dispensingOverviewData;
    
    
      useEffect(() => {
        getFilterData(year);    <--- This one is getting to and from data from server
      }, [getFilterData, year]);
    
      useEffect(() => {
        const to = defaultMonth.to;          <-- Here I am setting value to localState
        const from = defaultMonth.from;
        setDataTo(to);
        setDataFrom(from);
      }, [defaultMonth]);
    
      useEffect(() => {
        getOverviewData(year, dataTo, dataFrom, dataType);   <-- Here i am passing value to other action
      }, [getOverviewData, year, dataTo, dataFrom, dataType]);

我将其添加到localState中,因为一旦安装了组件,用户就可以更改to和from的值。

0 个答案:

没有答案