Comments (13)
Yeah the scenario is a reasonable one, I think I would prefer to go with a hook creator, exported as a named export from the package, something along the lines of:
import { makeUseAxios } from 'axios-hooks'
const useAxios = makeUseAxios({ ...base configuration })
What do you think?
from axios-hooks.
Hey @simoneb, I've create a sandbox as well, you can have a look at it whenever you want.
I've tried useAxios
as well, and it has the same exact problem, but if I switch back to the previous version everything works fine.
It has to be something about this latest version.
from axios-hooks.
can you explain what would be the use case you're trying to accomplish?
from axios-hooks.
For example, in one of the projects I'm working on now, we have to make AJAX
requests to both our private API
and another external service.
To accomplish this task I usually create two custom instances of Axios
, something along the lines of this:
ourApi.js
import axios from 'axios';
export default axios.create({
baseURL: 'http://our-api.com'
});
externalService.js
import axios from 'axios';
export default axios.create({
baseURL: 'http://external-service.com',
params: {
appid: 'mysecretkey'
}
});
Now, without axios-hooks
I would have imported the right instance in the needed component and that's it.
BUT, with axios-hooks
we need to configure
the hook
prior using it.
But ATM there is no way to reference/use a different instance
at my will, hence I opened this issue.
Thoughts?
from axios-hooks.
I'm working on a PR to add this, please check it out and let me know if you have thoughts. #100
from axios-hooks.
Hey, @simoneb , thanks for the quick P.R
.
I'd like to have a glimpse of how we are supposed to makeUseAxios
.
Do we have to configure it once?
Do we have to use makeUseAxios
and the returned useAxios
multiple times?
from axios-hooks.
@CloudPower97 see #100 and the updated README which includes an example.
To answer your questions:
makeUseAxios
is basically a Hook creator. You get back auseAxios
Hook that you can reuse, configured with the provided axios/cache supplied to the creator- you can use the so-created Hook as many times as you want
I published a prerelease 1.8.0-0 version on NPM so you can try it out.
Would this work for you?
from axios-hooks.
@CloudPower97 do you have any thoughts on this?
from axios-hooks.
Hey, @simoneb, sorry for this late answer.
I finally had a chance to try version 1.8.0-0
.
I've forked your CodeSandbox example and things went really smooth.
However, on the aforementioned project, I have got the following error:
Expected 0 type arguments, but got 1.ts(2558)
The code that is throwing this error is:
useApi<Colture[]>(
{
url: '/diz-coltures',
...config
},
options
);
where useApi
is what makeUseAxios
gives me back, and Colture
is a TS
interface
.
Worth noting that useAxios<Colture[]>
is not throwing this error.
Anyway, thank you for your time, I do really appreciate!
from axios-hooks.
Can you please post the error in full? I can't see what the compiler is complaining about.
from axios-hooks.
@CloudPower97 thanks for checking. There was indeed a problem with the TS type definition. Fixed in 1.8.0-2. Can you update and try again?
from axios-hooks.
@simoneb Everything works fine now, thank you!
from axios-hooks.
@CloudPower97 thanks for helping to troubleshoot the issue! I'll release this feature soon.
from axios-hooks.
Related Issues (20)
- Release pending!
- Refetch "expression is not callable" HOT 2
- Why I'm getting 2 rendered using this hook, when component mounts? HOT 9
- Release pending!
- Prevent CanceledError and redundant HTTP calls for identical request data HOT 1
- Support for API polling HOT 1
- type error HOT 9
- Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) HOT 1
- How to configure SSR with NextJS? HOT 3
- "Uncaught runtime errors: Cannot read properties of undefined (reading 'data')" after upgrading to CRA 5.0.1 & Node 18 HOT 6
- Not usable within the US? HOT 1
- Error from a dynamically loaded uri HOT 3
- Release pending!
- Release pending!
- useAxios hook sending in 2 requests, the first is cancelled immediately HOT 3
- Release pending! HOT 3
- Release pending!
- Release pending!
- Type error: MouseEvent using refetch and provided example from docs HOT 2
- Still an issue with both `executeGet` and `execute` methods when executed manually HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios-hooks.