What are forward refs in React js?

DWQA QuestionsCategory: Web Design HTMLWhat are forward refs in React js?
Editor Staff asked 1 week ago

What are forward refs in React js?

1 Answers
Editor Staff answered 1 week ago

Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child.

const ButtonElement = React.forwardRef((props, ref) => (
  <button ref={ref} className="CustomButton">
    {props.children}
  </button>
));

// Create ref to the DOM button:
const ref = React.createRef();
<ButtonElement ref={ref}>{'Forward Ref'}</ButtonElement>