I have a simple scenario: A parent class component and a child stateless component, and I would like to invoke the function defined in the child component directly from the parent. The solution that I came across here looks like this:
const { forwardRef, useRef, useImperativeHandle } = React;
// We need to wrap component in `forwardRef` in order to gain
// access to the ref object that is assigned using the `ref` prop.
// This ref is passed as the second parameter to the function component.
const Child = forwardRef((props, ref) => {
  // The component instance will be extended
  // with whatever you return from the callback passed
  // as the second argument
  useImperativeHandle(ref, () => ({
    getAlert() {
      alert("getAlert from Child");
    }
  }));
  return <h1>Hi</h1>;
});
const Parent = () => {
  // In order to gain access to the child component instance,
  // you need to assign it to a `ref`, so we call `useRef()` to get one
  const childRef = useRef();
  return (
    <div>
      <Child ref={childRef} />
      <button onClick={() => childRef.current.getAlert()}>Click</button>
    </div>
  );
};
ReactDOM.render(
  <Parent />,
  document.getElementById('root')
);
but the issue here is that I have those components defined in different files, and I don't know how can I get ahold of useRef if I declare it in the child component, and it doesn't really look readable anyway. Can you guys help me out with this, or provide another approach ? Thanks.
