简体   繁体   English

如何调用具有参数的同名function

[英]How to call a function which has a same name with a parameter

I am new to react.我是新来的反应。 I wanna call a function from other.js file which has the same name with the parameter in the dashboard function.我想从 other.js 文件中调用 function,该文件与仪表板 function 中的参数同名。 But it shows TypeError: getCurrentProfile is not a function.但它显示 TypeError: getCurrentProfile is not a function。 Any tips and helps are much appreciated in advance!提前非常感谢任何提示和帮助!

for example:例如:

import { getCurrentProfile } from '../actions/profile';

const Dashboard = ({
getCurrentProfile,
auth: { user }

}) => {
useEffect(() => {
    getCurrentProfile();
}, []);
return(
    <Fragment>
        <h1> DAshboard</h1>
        <i className='das fa-user'></i><p>Welcome {user && user.name}</p>
    </Fragment>
    );
};

error: TypeError: getCurrentProfile is not a function错误: TypeError:getCurrentProfile 不是 function

You'll need to make it so that there's no name collision.您需要做到这一点,以免发生名称冲突。 You could rename the import, or rename the destructured prop, or don't destructure the props at all.您可以重命名导入,或重命名解构的道具,或者根本不解构道具。

Actually, it doesn't look like the getCurrentProfile prop is used at all, so you could just remove it from the parameter list:实际上,它看起来根本没有使用getCurrentProfile ,因此您可以将其从参数列表中删除:

const Dashboard = ({
    auth: { user }
}) => {
    useEffect(getCurrentProfile, []);
    return (
        <Fragment>
            <h1> DAshboard</h1>
            <i className='das fa-user'></i><p>Welcome {user && user.name}</p>
        </Fragment>
    );
};

If you actually are using the getCurrentProfile prop somewhere, then you could do如果您实际上在某处使用getCurrentProfile道具,那么您可以这样做

const Dashboard = (props) => {
    const { user } = props.auth;
    useEffect(getCurrentProfile, []);

You can use "as" in your import.您可以在导入中使用“as”。

import { getCurrentProfile as Alias } from '../actions/profile';

and than use that Alias name in your hook而不是在你的钩子中使用那个别名

useEffect(() => {
    Alias();
}, []);

暂无
暂无

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

相关问题 如何创建具有相同名称的属性和函数的类 - How to create a class which has a property and a function with same name 如何通过作为参数传递的名称调用函数? - How to call a function by its name, passed as parameter? Jquery:如何在第一个参数完成后用不同的参数回调相同的函数? - Jquery: How can I call back the same function with a different parameter after the 1st one has finished? 如何多次同时运行 function,将参数传递给具有相同 class 名称的 div - How to run a function simultaneously multiple times, passing parameter to divs that has the same class name 如何在javascript中使用不同的参数调用相同的函数? - how to call same function with different parameter in javascript? 如何采用表单名,提交时在此jscript函数中调用javascript函数,然后在同一js函数中使用name作为变量 - How to take form name, from which, on submit, is call javascript function in this jscript function and then use name in same js function as variable 如何在另一个文件上调用具有相同名称的函数 - How to call a function with same name on another file 如何调用在另一个函数中作为参数传递的函数 - how to call a function which is passed as parameter in another function 我将一个函数(本身具有一个参数)作为参数传递给另一个函数,以及如何获取该参数 - I passed a function(which itself has a parameter) as a parameter into another function and how to get the parameter 如何重用具有相同功能但变量名称不同的代码 - How to reuse code which has same functionality but variable name differing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM