我正在开发一个应用程序,该应用程序从服务器端获取to
和from
的值,为此我在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的值。