Coder Social home page Coder Social logo

pass params to execute about axios-hooks HOT 8 CLOSED

simoneb avatar simoneb commented on July 21, 2024
pass params to execute

from axios-hooks.

Comments (8)

simoneb avatar simoneb commented on July 21, 2024 3

The getUser function will take a request configuration that is the same as you would provide to axios. So:

const [{ data, loading, error }, getUser] = useAxios(
    // note I removed the query string from the url
    'https://api.github.com/search/users',
    {
      manual: true
    }
);

// them some time later

getUser({ params: { q: username } })

from axios-hooks.

morriq avatar morriq commented on July 21, 2024 3

👍 for passing dynamic segment of url in manual invoke function.

something like:

/api/users/:userId

and then run({ userId })

from axios-hooks.

simoneb avatar simoneb commented on July 21, 2024 1

@muhsalaa you're welcome. Yes, when changing any of the parameters of the hook, the request is re-executed automatically without needing to invoke the execute function.

from axios-hooks.

flaviolivolsi avatar flaviolivolsi commented on July 21, 2024

@simoneb what about params in the URL? How to pass the param in that case?

Example:

const [{ data, loading, error }, getUser] = useAxios(
    'https://api.github.com/user/${username}',
    {
      manual: true
    }
);

from axios-hooks.

simoneb avatar simoneb commented on July 21, 2024

@flaviolivolsi one easy option would be to have username in the component state and use it in the Hook like this:

const [username, setUsername] = useState()

const [{ data, loading, error }, getUser] = useAxios(
   `https://api.github.com/user/${username}`,
    {
      manual: true
    }
);

// then somewhere in your code
setUsername('johndoe')

Depends on the use case but you get the idea

from axios-hooks.

muhsalaa avatar muhsalaa commented on July 21, 2024

Hi, I got the same issue here. I successfully fetched with params in options

{
    url: '/invitation/templates',
    method: 'GET',
    params: {type: typeState}
}

however, when I changed the typeState after choosing an option, and then refetch, I got the error below:

Screen Shot 2020-10-18 at 21 40 56

thought my fetch is still continued (the result I want still returned), I find difficulty when using an error interceptor that rejects the promise.

export function interceptorResponseError(error) {
  const response = error.response?.data?.error || [
    {
      code: 'unknown',
      message: 'There is something wrong. Please try again later.',
    },
  ];
  return Promise.reject(response);
}

now my error alert in my component will be shown because of this error. How can I get rid of the error response in the first image? where does the error come from?

from axios-hooks.

simoneb avatar simoneb commented on July 21, 2024

The error you see is a cancellation error, meaning that you're doing multiple requests and one is canceling the other. This library does cancellation automatically.

This suggests a user-land bug. Can you provide a repro?

from axios-hooks.

muhsalaa avatar muhsalaa commented on July 21, 2024

actually, I'm able to solve the problem now. When you said it because of multiple requests, I realize that my code is wrong. My mistake is I use refetch after changing the state.

const [type, setType] = useState('');

const [
    { data: vData, loading: vLoading, error: vError },
    refetch,
  ] = useRequest({ ...BuilderAPI.getInvitationTemplate, params: { type } });

I was thinking that I need to refetch after set a new type. Turns out it will automatically refetch when deps changes. Thanks for your response

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.