Problem: UseEffect of Child widget is getting executed thrice even though i have given empty dependency array. For simplicity of code I have omitted some logic in Parent component that loads childWidget dynamically. If needed I can add that too here but is there any issue like conflicts of forwardRef and UseEffect Or something else is missing.
Code:
const ChildWidget = (props) =>{
useEffect(()=>{ console.log('ChildWidget useEffect func')},[])
return (<div>ChildWidget</div>)
}
const Parent = forwardRef((props, ref) =>{
useImperativeHandler(ref,()=>({
show(){.. //some code ..}
hide(){.. //some code ..}
}))
useEffect(()=>{console.log('Parent UseEffect')},[]);
return(
<ChildWidget a={props.a} b={props.b}/>
)
})
Result:
UseEffect of Parent is always called once but of that of child it is called thrice.
2
Answers
Try this once…
In my knowlegde, issue with the useEffect in the ChildWidget component being called multiple times is because of a syntax error. The empty dependency array [] should be passed as the second argument to useEffect, not as an argument to the function itself.