[英]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.