如何使用参数调用同名函数

问题描述

我是新手。我想从其他 .js 文件调用一个与仪表板函数中的参数同名的函数。但它显示 TypeError: getCurrentProfile is not a function。非常感谢任何提示和帮助!

例如:

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>
    );
};

错误类型错误:getCurrentProfile 不是函数

解决方法

您需要确保不会发生名称冲突。您可以重命名导入,或重命名解构的道具,或者根本不解构道具。

实际上,它看起来根本没有使用 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>
    );
};

如果您确实在某处使用了 getCurrentProfile 道具,那么您可以这样做

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

您可以在导入中使用“as”。

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

然后在钩子中使用别名

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