I want my React functional component to call a function in its parent.
I was able to do this with class components like so:
<MyChild func={this.functionname} />
But I'm having trouble access this.functionName withtin my child component, which is a functional component.
How can I access this in the child functional component, *as well as within all of its functions (e.g. useCallback and useEffect)
I'm not sure I understand the question correctly but you can call a function from the parent component inside the child easily like so:
export function Child({ func }) {
  useEffect(() => {
    console.log('Calling func');
    func();
  }, [func]);
  return <div>I am the child component</div>;
}
export function Parent() {
  const childFunc = () => console.log('call me!');
  return (
    <main>
      <h1>Hello world</h1>
      <Child func={childFunc} />
    </main>
  );
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With