Skip to content

Instantly share code, notes, and snippets.

@harshaktg
Created November 19, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshaktg/9fdedd43932c3f5ab37abad7444ca9e9 to your computer and use it in GitHub Desktop.
Save harshaktg/9fdedd43932c3f5ab37abad7444ca9e9 to your computer and use it in GitHub Desktop.
SWR implementation
import useSWR from "swr";
import "./App.css";
function App() {
// Fetcher function
const fetcher = (...args) => fetch(...args).then((res) => res.json());
// SWR implementation
const { data, error } = useSWR(
"https://jsonplaceholder.typicode.com/posts/1",
fetcher
);
if (error) return <div>Failed to load</div>;
if (!data) return <div>Loading</div>;
// render data
return (
<div className="App">
<h2>{data.title}</h2>
<p>{data.body}</p>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment