I am working on an app which need to fetch data from api and change data on user input. Here the problem is when user first mount the page they should send a default value monthly and 2020. But if user made any changes and if they refresh page it should send user updated data. like if user selected 'half-yearly' and 2018 then on refresh it should send half-yearly and 2018.
I am not sure if it is possible in reacjs or not.
I have tried doing this but it is not working it goes by monthly and 2020 every time I refresh page.
Here is my code
const DispensingIncidents = (props) => {
const classes = useStyles();
const {
getFilterData,
dispensingData,
getPeriodList,
getOverviewData,
location,
history,
} = props;
const [timeSpan, setTimeSpan] = React.useState("Monthly");
const [year, setYear] = React.useState(2020);
const [tabValue, setTabValue] = React.useState(0);
const [spanData, setSpanData] = React.useState([]);
const { loading, duration, period, dispensingOverviewData } = dispensingData;
const { count } = dispensingOverviewData;
useEffect(() => {
getPeriodList();
}, [getPeriodList]);
useEffect(() => {
history.replace({
pathname: location.pathname,
search: `?year=${year}&period=${timeSpan}`,
});
setYear(year);
setTimeSpan(timeSpan);
// eslint-disable-next-line
}, [year, timeSpan]);
useEffect(() => {
getFilterData(year);
}, [getFilterData, year]);
function useQuery() {
return new URLSearchParams(location.search);
}
const query = useQuery();
// eslint-disable-next-line
const handleTabChange = (event, newValue) => {
setTabValue(newValue);
};
const handleYearChange = (event) => {
setYear(event.target.value);
setTimeSpan(query.get("period"));
};
const handleSpanChange = (event) => {
const value = event.target.value;
setTimeSpan(value);
};
const time = query.get("period");
useEffect(() => {
if (time === "Yearly") {
const yearlyData = duration["yearly"];
setSpanData(yearlyData);
} else if (time === "Weekly") {
const weeklyData = duration["weekly"];
setSpanData(weeklyData);
} else if (time === "Quarterly") {
const quarterlyData = duration["quarterly"];
setSpanData(quarterlyData);
} else if (time === "Monthly") {
const monthlyData = duration["monthly"];
setSpanData(monthlyData);
} else if (time === "6 months") {
const halfYearlyData = duration["half-yearly"];
setSpanData(halfYearlyData);
}
}, [time, duration]);
Any help would be great.