Giter VIP home page Giter VIP logo

Comments (13)

simoneb avatar simoneb commented on June 12, 2024 1

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.

CloudPower97 avatar CloudPower97 commented on June 12, 2024 1

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.

simoneb avatar simoneb commented on June 12, 2024

can you explain what would be the use case you're trying to accomplish?

from axios-hooks.

CloudPower97 avatar CloudPower97 commented on June 12, 2024

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.

simoneb avatar simoneb commented on June 12, 2024

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.

CloudPower97 avatar CloudPower97 commented on June 12, 2024

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.

simoneb avatar simoneb commented on June 12, 2024

@CloudPower97 see #100 and the updated README which includes an example.

To answer your questions:

  • makeUseAxios is basically a Hook creator. You get back a useAxios 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.

simoneb avatar simoneb commented on June 12, 2024

@CloudPower97 do you have any thoughts on this?

from axios-hooks.

CloudPower97 avatar CloudPower97 commented on June 12, 2024

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.

simoneb avatar simoneb commented on June 12, 2024

Can you please post the error in full? I can't see what the compiler is complaining about.

from axios-hooks.

simoneb avatar simoneb commented on June 12, 2024

@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.

CloudPower97 avatar CloudPower97 commented on June 12, 2024

@simoneb Everything works fine now, thank you!

from axios-hooks.

simoneb avatar simoneb commented on June 12, 2024

@CloudPower97 thanks for helping to troubleshoot the issue! I'll release this feature soon.

from axios-hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.