I am trying to pass a function UpdateHero into a component, then from that component pass it into another component.
When the function reaches the last component it is for some reason no longer a function and this error appears:
UpdateHero is not a function
it works fine in the first component that it is passed into, but I cannot figure out how to pass it into the second one properly.
information.js:
import { useHero } from "./hero";
import HeroButtons from './hero-buttons';
export default function HeroesComponent(){
let { hero, UpdateHero } = useHero();
return (
<div>
<HeroButtons hero = {hero} UpdateHero = {UpdateHero}/>
</div>
)
}
hero-buttons.js:
import HeroDetails from './hero-detail';
export default function HeroButtons({hero, UpdateHero}) {
return (
<div>
<HeroDetails hero = {hero} UpdateHero = {UpdateHero}/>
</div>
)
}
hero-detail.js:
export default function HeroDetails(hero, UpdateHero) {
UpdateHero(something)
}
in hero-detail I get the error: UpdateHero is not a function
this is a very cut-down version of the program, the hero appears to work fine when I pass it into hero-detail so I’m thinking its a problem with hero buttons when I’m trying to pass it down
4
Answers
should be
since
props
is always an objectyou should receive UpdateHero function in your HeroDetails as
export default function HeroDetails({hero, UpdateHero}) {
};
you should write below formate in heo-detail.js
export default function HeroDetails({hero, UpdateHero}) {….}
they way the props have been declared inside the hero-detail.js component is wrong i guess, it should be like
instead of
By the way you declared, it should work if you call the function by using
Because the first argument which gets passed down to that component is for props.