useSWR best react hook fetch library!

What is SWR?

Features

how to use?

npm install swr //with npm yarn add swr // or with yarn
//Example 1import React from 'react'
import useSWR, { mutate } from "swr";
const fetcher = (...args) => fetch(...args).then((res) => res.json());export default function Component1() {const { data, error } = useSWR("http://localhost:8000/server/get", fetcher, {});
if (!data) return "I am loading...";
if (error) return "there is an error"; const users = data.map(user => { return <li>{user}</li>;});return (<ol>{users}</ol>)}
//Example 2import React from 'react'
import useSWR, {mutate} from "swr";
const fetcher = (...args) => fetch(...args).then((res) => res.json());export default function Component1() {const { data, error } = useSWR("http://localhost:8000/server/get", fetcher, { revalidateOnFocus: false, refreshInterval : 2000});if (!data) return "I am loading...";if (error) return "there is an error";const users = data.map(user => {
return <li>{user}</li>;
});
return (
<ol>{users}
<button onClick={
async () => {
await fetcher("http://localhost:8000/server/post", {
headers: {'Content-Type': 'application/json'},
method: "POST", body: JSON.stringify({ name:"karimp" }),});
mutate("http://localhost:8000/server/get");
}}
> Click</button>
</ol>
)}
useSWR("http://localhost:8000/server/get" .....

More option ( from the official Github repo ):

Software Engineer