Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - 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. But it shows 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

question from:https://stackoverflow.com/questions/65644109/how-to-call-a-function-which-has-a-same-name-with-a-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...