Mutate revalidate with query params #505
-
Hello there, const { data } = useSWR('/events')
const [queryParams, setQueryParams] = useState('')
useEffect(() => {
const events = mutate(`/events?${queryParams}`)
}, [queryParams]) I would like to know how to revalidate my call using I also tried to write this code, without success:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
What you should do is to pass the query to useSWR like this: const [queryParams, setQueryParams] = useState("");
const { data } = useSWR(`/events?${queryParams`); This way, every time queryParams change you will trigger a new render, and you don't need to call mutate. |
Beta Was this translation helpful? Give feedback.
-
How we can handle multiple query parameters? for example with optional ones? |
Beta Was this translation helpful? Give feedback.
-
in my case i do it like this way:
|
Beta Was this translation helpful? Give feedback.
-
In my case I use @sergiodxa 's approach for the hook, together with the rendering the component based on conditions as in the snippet below. However, the re-rendering makes the component flickering with the How can I avoid that and render the rest of the page, while modifying the query parameters? I need to mention that some other functions I declare require the
|
Beta Was this translation helpful? Give feedback.
What you should do is to pass the query to useSWR like this:
This way, every time queryParams change you will trigger a new render, and you don't need to call mutate.