D
D
dc65k2020-05-16 12:24:41
React
dc65k, 2020-05-16 12:24:41

How to fix `ref` is not a prop error?

Hello everyone, using the following approach, an error occurs.

index.js:1 Warning: [object Object]: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://fb.me/react-special-props)

Please tell me how to correct it?
https://reactjs.org/docs/forwarding-refs.html
import React from "react";

const { useRef, forwardRef } = React;

const ComponentOne = forwardRef((props, ref) => {
  return (
    <div ref={ref}>
      <div>ComponentOne</div>
    </div>
  );
});

const ComponentTwo = node => {
  console.log(node);
  return (
    <div>
      <div>ComponentTwo</div>
    </div>
  );
};

export const App = () => {
  const node = useRef(null);

  return (
    <div>
      <ComponentOne ref={node} />
      <ComponentTwo node={node} />
    </div>
  );
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question