Skip to content

Instantly share code, notes, and snippets.

@zmaplex
Created April 24, 2024 09:54
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 zmaplex/6c4d4eacea1df7fcfccf3fe4c17f4a0a to your computer and use it in GitHub Desktop.
Save zmaplex/6c4d4eacea1df7fcfccf3fe4c17f4a0a to your computer and use it in GitHub Desktop.
import { useState } from 'react';
import useSWR from 'swr';
const fetchMoreData = async (count: number) => {
await new Promise((resolve) => setTimeout(resolve, count * 100));
return count
}
export default function Demo() {
const [count, setCount] = useState(0);
const { data, error, isLoading } = useSWR(`count-${count}-swr`, async () => {
if (count == 5) {
throw new Error("count is 5")
}
return await fetchMoreData(count)
})
const handleClick = () => {
setCount(0);
};
if (isLoading) return <div>Loading...</div>
if (error) return <div>{`${error}`}</div>
return (<div >
<div>{data}</div>
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={handleClick}>handleClick</button>
</div>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment