-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use useSwr when I was clicking on a save button to update something? #254
Comments
Do you mean not start the fetching until you click a button? If that's your use case you can pass function FetchOnClick() {
const [shouldFetch, setShouldFetch] = React.useState(false);
const { data } = useSWR(shouldFetch ? null : "/api/users/1", fetcher);
function handleClick() {
setShouldFetch(true);
}
return (
<>
<button disable={shouldFetch} onClick={handleClick}>Fetch</button>
{data ? <h1>{data.fullName}</h1> : null}
</>
);
} Something like that, another option is to move the data fetching and the UI rendered with that data to a second component and use the handleClick to control if you should render it or not. function RenderOnClick() {
const [shouldRender, setShouldRender] = React.useState(false);
function handleClick() {
setShouldRender(true);
}
return (
<>
<button disable={shouldFetch} onClick={handleClick}>Fetch</button>
{shouldRender && <FetchAndRender />}
</>
);
}
function FetchAndRender() {
const { data } = useSWR("/api/users/1", fetcher);
return (
<>
{data ? <h1>{data.fullName}</h1> : null
</>
);
} Both ways will only start fetching after you click the button, I would personally go with the second way. |
Thanks a lot |
I would also go with the first example, It doesn't force you to split code into different chunks when not needed. The first example also allows for a better separation of logic and view data which is always good! |
@sergiodxa what if I have to pass parameters to fetcher function on click too? |
Seems strange that this is still the best way to do this |
Please take a look at #1450 and give feedback — we are working on a new hook for it 🙏 |
@anabeatrizzz did you find anything about how to pass parameters to fetcher on click? I need to do that and i'm wondering how. Tried to use useState but it didn't work properly. |
@venuziano you can have a look at the documentation that explains about arguments. |
You should use useSWRMutation instead.
Conditionally setting the URL looks hacky, IMHO: |
I would recommend using fetch for mutations instead. const { data } = useSWR(shouldFetch ? null : "/api/users/1"); We us that pattern a lot because we often don't have all the data for the fetch on pageload.And you can't do |
I'm using export function useCalculate() {
let theURL = null
theURL = `/api/calculate?`
let { data, trigger } = useSWRMutation(
theURL,
(url: string, { arg }: any) => {
// console.log('fetch arg', arg)
return fetch(`${BaseURL}${url}${new URLSearchParams(arg)}`).then((res) =>
res.json()
)
}
)
return {
trigger,
data,
}
} |
Is there a benefit to this approach instead of just doing: onClick = () => fetch(`${BaseURL}${url}${new URLSearchParams(arg)}`) |
sure you can use this approach but still you have to handle |
so in my case, I need to call the API the first time, and then I need to recall it when I click on a children's component button Any suggestion on how to handle this? Appreciate any help |
No description provided.
The text was updated successfully, but these errors were encountered: