1. How does it work?
SWR has been configured in the folder:app/api/globalFetcher.ts
To use SWR on a page, you need to import it
and make a call. After that, you need to make calls to swr using http.get('path') or http.post('path') see below
implementation.
import useSWR from 'swr';
import { uniqueId } from 'lodash';
import { sub } from 'date-fns';
const API_URL = '/api/data/chat/ChatData';
export const getFetcher = (url: string) =>
fetch(url).then((res) => {
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return res.json();
});
export const useChats = () => {
const { data, error } = useSWR(API_URL, getFetcher);
return {
data,
error,
isLoading: !data && !error,
};
};
{Object.keys(data).map((key, index) => {
return (
...
...
);
...
...
})
};