I want the data in the UI to update immediately rather than waiting for SWR to sync with the database.
I've tried following the docs, but the UI still won't update automatically (I switch away and back to my browser to update it). Here's the code:
import useSWR, { useSWRConfig } from "swr";
export default function Tasks() {
const { mutate } = useSWRConfig()
const { data } = useSWR("/api/tasks", (...args) =>
fetch(...args).then((res) => res.json())
)
const deleteTask = async (taskId) => {
const newData = data.filter((task) => task.id !== taskId)
mutate(`/api/tasks/${taskId}`, newData, false)
await fetch(`/api/tasks/${taskId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
})
mutate(`/api/tasks/${taskId}`)
}
if (!data) return <p>loading</p>
return (
<div>
{data.map((task) => (
<div key={task.id}>
<div>{task.content}</div>
<button onClick={() => deleteTask(task.id)}>Delete</button>
</div>
))}
</div>
)
}