Skip to content

Dynamic parameters in useMutation #1337

@adeepika1991

Description

@adeepika1991

I would like to reuse my fetch function inside useMutation hook in different places in my react app. Each fetch function has different url endpoints and data. While I can pass data inside mutate function, how do I pass other parameters like url, http methods ?

I tried passing url parameter, I am getting
index.js:1 SyntaxError: Unexpected token W in JSON at position 0

My fetch function is as follows

const postPhone = async (data, url) => {
    const requestOptions = {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    };
    const response = await fetch(
       permanentUrl + url ,
        requestOptions
    );

    const otpResponse = await response.json();
    return otpResponse;

}

If I have to use a static url inside the function with no url parameter then mutate function of useMutation is working absolutely fine. But then I have to write multiple postPhone function many times. Is there any solution for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions