I am working on a app which get to
and from
value from server side for that i dispatch action in one of the useEffect
and after getting the value I pass it to setState inside the useEffect but it is not working. I have no idea why.
Here how i have did this
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]);
I am adding this to localState because once the component is mount user can chanage the value of to and from as well.