I try to display data and I use swr data fetching with react and here is the code :
import useSWR from "swr";
import axios from "axios";
const fetcherFunc = (...args) => {
return axios(...args).then((res) => console.log(res.data));
};
function MyComponent() {
const { data, error } = useSWR(
"https://jsonplaceholder.typicode.com/posts/",
fetcherFunc
);
if (error) return <div>failed to load</div>;
if (!data) return <div>loading...</div>;
return <div>{JSON.stringify(data)}</div>;
}
export default MyComponent;
The problem is that it's stuck in loading and the data won't get updated and is undefined also the console log of data in fetcher appears to work .
How to fetch data with swr in react and display fetched data ?