简体   繁体   English

如何在不使用函数参数的情况下将值传递给函数?

[英]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:我有一个返回 React Query 突变钩子的工厂函数,如下所示:

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. useMutation的常用变异函数是(params: any) => any ,我必须对它进行 curry 以将 axios 传递给用户。 I want to see if it's possible to have axios inside of mutationFunction without currying.我想看看是否可以在mutationFunction中使用axios 而无需进行柯里化。

The idea I have is to use prototypes, and write wrapper functions for every axios http method.我的想法是使用原型,并为每个 axios http 方法编写包装函数。 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 .我一直坚持将 axios 转换为Mutation Ideally, axios isn't passed to the user, so I have to set axios inside useMutationFactory .理想情况下, axios 不会传递给用户,因此我必须在 useMutationFactory 中设置useMutationFactory I can't use apply() or call() , since the user would have to call that (I'm assuming).我不能使用apply()call() ,因为用户必须调用它(我假设)。 I'm still very new at Object prototyping, so I would appreciate any suggestions or help!我对对象原型还是很陌生,所以我将不胜感激任何建议或帮助!

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.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不调用 function 的情况下将参数传递给 function? - How to pass parameters to function without calling function? 如何在不使用全局变量的情况下将变量传递给字符串化函数? - How can I pass a variable to a stringified function without using globals? 我无法使用addEventListener Javascript / Jquery将参数传递给函数 - I can not pass parameters to a function using addEventListener Javascript/Jquery 如何将值传递给 javascript function? - How can I pass a value to a javascript function? 我如何将参数传递给函数以获取当前样式 - how can i pass a parameters to a function to get current Style 我如何将参数传递给点击事件列表内的 function - How can i pass parameters to a function inside click eventlistner js sort()自定义函数如何传递更多参数? - js sort() custom function how can i pass more parameters? 如何在反应中使用导航 function 将参数传递给路由? - How can I pass parameters to route with navigate function in react? 如何将对象参数传递给反引号中的onclick函数 - How can I pass object parameters to the onclick function in backtick 如何使用Excel VBA将变量参数传递给Javascript函数 - How can I pass variable parameters into a Javascript Function using Excel VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM