I am trying to loop get API, the API takes a perameter and returns the is the parameter true or false.
I have used useEffect hook and looping the settingTypes and inside the loop I am calling the API and from the response, I am storing the values. I don't want to do like the below code because when the getSettingValues API is updated it's not re-fetching the API,
const [activityTypes, setActivityTypes] = useState<any>([]);
useEffect(() => {
async function fetchData() {
settingTypes.map(async (item: any) => {
return await Api.user
.getSettingValues(item.channelSettingTypeId)
.then((response: any) => {
setActivityTypes((oldArray: any) => [
...oldArray,
{[item.channelSettingTypeId]: response.settingValue},
]);
});
});
}
fetchData();
}, [activityFeedData.activityList, settingTypes]);
I am trying to do it using react query. Below the code is react query hook, useGetSettingValues receives a parameter and returns is the parameter true or false.
const useGetSettingValues = (params: any) =>
useQuery([SETTING_VALUE, params], () => Api.user.getSettingValues(params), {
initialData: {data: ''},
});
My major issue is I want to loop the useGetSettingValues API and store all the response data in a state. as like the below code
await Api.user
.getSettingValues(item.channelSettingTypeId)
.then((response: any) => {
setActivityTypes((oldArray: any) => [
...oldArray,
{[item.channelSettingTypeId]: response.settingValue},
]);
});
Any Idea how can I achieve it?