Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
How can I pass a value into a function without using function parameters?

I have a factory function that returns a React Query mutation hook, like this:

function useMutationFactory(mutationFunction: (axios: Axios) => (params: any) => any) {
    const baseURL = getBaseURL(); // Retrieves the Base URL (this is dynamic)
    const axios = useAxios({baseURL}); // Creates an Axios instance with the URL
    ...
    return useMutation(mutationFunction(axios));
}

The usual mutation function for useMutation is (params: any) => any, I have to curry it to pass axios to the user. I want to see if it's possible to have axios inside of mutationFunction without currying.

The idea I have is to use prototypes, and write wrapper functions for every axios http method. Something like this:

const Mutation = {
    queryGet: (...params: Parameters<Axios["get"]>) => {
        const axios = this.prototype.axios; // Not sure about this part
        return axios.get(...params);
    }
}

The mutation function from users would be something like this:

const mutation = useMutationFactory((params) => {
     const res = Mutation.queryGet("/");
});

I'm stuck on getting axios to Mutation. Ideally, axios isn't passed to the user, so I have to set axios inside useMutationFactory. I can't use apply() or call(), since the user would have to call that (I'm assuming). I'm still very new at Object prototyping, so I would appreciate any suggestions or help!

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Well, have the function do something with a variable in the global scope, update / change the variable in the global scope to the value you want to pass to the function, then call the function so that it reads the updated value of the variable.

I'm on my phone atm else I would give a code example. Hope this helps though.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda