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