-
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
Example for axios #143
Comments
When using an array as the key, SWR will pass those items as arguments to the fetcher function: useSWR([A, B, C, ...], fetcher)
// calls fetcher(A, B, C, ...) For your specific case, I'd do something like: // global config because this part won't change
const getRequest: AxiosRequestConfig = {
baseURL: 'https://api.example.com',
withCredentials: true,
method: 'GET'
};
function App () {
// inside component
const params = useMemo(() => ({ term }), [term])
useSWR(
['/search', params],
(url, params) => axios({ ...getRequest, url, params }).then(res => res.data)
)
} or simpler: // global config because this part won't change
const getRequest: AxiosRequestConfig = {
baseURL: 'https://api.example.com',
withCredentials: true,
method: 'GET'
};
const fetchTerm = (url, term) =>
axios({ ...getRequest, url, params: { term } }).then(res => res.data)
function App () {
// inside component
useSWR(['/search', term], fetchTerm)
} The major reason of shallow comparison is performance. So it will always render faster, and it will reuse the stale data if the same url + params pair has occurred before. Thanks for the feedback! We will definitely improve the readme and create an example for axios. |
@quietshu Thank you for a quick reply! In your example you have pulled the |
@Svish I see your point 👍 I think this might be helpful then: // let url = '/search'
// let term = 'foobar'
useSWR([url, term], () => axios({
baseURL: 'https://api.example.com',
withCredentials: true,
method: 'GET',
url,
params: { term }
}).then(res => res.data))
|
So, would something along the following work? 🤔 // useRequest.js
export default function useRequest(request, config) {
const { data: response, error, isValidating, revalidate } = useSWR(
JSON.stringify(request),
() => axios(request),
config
);
return {
data: response && response.data,
response,
error,
isValidating,
revalidate,
};
} const { data } = useRequest({
baseURL: 'https://api.example.com',
withCredentials: true,
method: 'GET',
url: '/search',
params: {
term: 'foobar',
},
}); |
It will work :) |
@quietshu Quick question... does |
@Svish nope, |
@Svish import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios"
import useSWR, { ConfigInterface } from "swr"
export const useSWRAxios = <Data extends {}>(
request: Omit<AxiosRequestConfig, "baseURL" | "method" | "data">,
config?: ConfigInterface,
) => {
const requestModified: AxiosRequestConfig = {
...request,
method: `GET`,
}
const configModified: ConfigInterface = {
...config,
// global customizations
}
/**
* Axios has a wrapper object around data => filter response
*/
const { data: response, ...responseUseSWR } = useSWR<
AxiosResponse<Data>,
AxiosError
>(
JSON.stringify(requestModified),
async () => axios(requestModified),
configModified,
)
const { data, ...responseAxios } = response || {}
return { ...responseUseSWR, responseAxios, data }
} |
@o-alexandrov If you check out the pr i added for this, I did add one for typescript 😉 https://github.com/zeit/swr/blob/master/examples/axios-typescript/libs/useRequest.ts |
As a noob end-user the |
How do you folks handle updating the cache with this Sorry, not trying to dump on the helper, I'm just worried this Issue will cause people to reach for the I'm new to SWR, so maybe I'm missing something. If so, I'd appreciate anyone educating me. 🙇 |
// useRequest.js return { give ex for post method @Svish ,please help . I am struck |
With
axios
it's handy to "define requests" using a request object, for example like this:But from what I can gather, the
key
I can pass in touseSWR
has to be a string? I can't find any of your included examples that uses anything other than a simple string either. In the readme, there's a short mention of using arrays, which can include both strings and objects, but it doesn't mention what the fetcher would actually look like, and it also says the key comparison is shallow, so with my request object above here, I assume that means a change inrequest.params.term
would not be picked up?Would it be possible for someone to add an example of how one can use
useSWR
with axios and a request object like this? Or is it perhaps not possible to do that?The text was updated successfully, but these errors were encountered: