Answer the question
In order to leave comments, you need to log in
Why doesn't calling a function passed through props work?
The parent component has this function
this.mobileMenuOpen = () => {
console.log('тут')
this.setState({
mobileMenuShow: !this.state.mobileMenuShow,
})
console.log(this.state.mobileMenuShow)
}
<BackgroundWave
link={this.waveBackground}
link2={this.waveBackgroundWrapper}
onMobileMenuOpen={this.mobileMenuOpen}
mobileMenuOpen={this.state.mobileMenuShow}
/>
onMobileMenuOpen={this.mobileMenuOpen}
function BackgroundWave(props) {
let classBackground, classArrow = '';
if(props.mobileMenuOpen) {
classBackground = 'background-wave background-wave--opened';
} else {
classBackground = 'background-wave';
}
return (
<div className={classBackground} ref={props.link2}>
<canvas ref={props.link} id='background-animation' className='background-wave__item'></canvas>
<div className='background-wave__opener-wrap' onClick={props.onMobileMenuOpen}>
<svg className='background-wave__opener'>
<use xlinkHref='img/icons/sprite.svg#mobile-opener' />
</svg>
</div>
</div>
);
}
onClick={props.onMobileMenuOpen()}
onClick={() => {props.onMobileMenuOpen}}
Answer the question
In order to leave comments, you need to log in
Passing a function and calling it looks correct. Perhaps the event onClick
on the element does not occur, due to the fact that the click itself goes to svg.
Do an experiment, do this:<div>
and check if there is a result in the log on click.
Show the full code of the parent component, is the mobileMenuOpen function declared in the constructor or what? If not, why is this there?
If the parent component is a class, then the method is declared like this:
If it is a functional component, then this:
Do this and it should work, or if it doesn’t work, put the full code in the sandbox
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question