Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid 'Function components cannot be given refs' when using react-router-dom?

I have the following (using Material UI)....

import React from "react"; import { NavLink } from "react-router-dom"; import Tabs from "@material-ui/core/Tabs"; import Tab from "@material-ui/core/Tab"; function LinkTab(link){     return <Tab component={NavLink}         to={link.link}         label={link.label}         value={link.link}         key={link.link}     />; } 

In the new versions this causes the following warning...

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Check the render method of ForwardRef. in NavLink (created by ForwardRef)

I tried changing to...

function LinkTab(link){     // See https://material-ui.com/guides/composition/#caveat-with-refs     const MyLink = React.forwardRef((props, ref) => <NavLink {...props} ref={ref} />);     return <Tab component={MyLink}         to={link.link}         label={link.label}         value={link.link}         key={link.link}     />; } 

But I still get the warning. How do I resolve this issue?

like image 220
JGleason Avatar asked Jun 06 '19 20:06

JGleason


People also ask

Why we may not use the ref attribute on function components?

When the ref attribute is used on a custom class component, the ref object receives the mounted instance of the component as its current . You may not use the ref attribute on function components because they don't have instances.

How do you pass ref to the functional component?

To create a ref in a functional component we use the useRef() hook which returns a mutable object with a . current property set to the initialValue we passed to the hook. This returned object will persist for the full lifetime of the component. Thus, throughout all of its re-rendering and until it unmounts.

Can you pass ref to component?

Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary for most components in the application. However, it can be useful for some kinds of components, especially in reusable component libraries.

What is forwardRef in React?

The forwardRef method in React allows parent components to move down (or “forward”) refs to their children. ForwardRef gives a child component a reference to a DOM entity created by its parent component in React. This helps the child to read and modify the element from any location where it is used.


1 Answers

NavLink from react-router is a function component that is a specialized version of Link which exposes a innerRef prop for that purpose.

// required for react-router-dom < 6.0.0 // see https://github.com/ReactTraining/react-router/issues/6056#issuecomment-435524678 const MyLink = React.forwardRef((props, ref) => <NavLink innerRef={ref} {...props} />); 

You could've also searched our docs for react-router which leads you to https://mui.com/getting-started/faq/#how-do-i-use-react-router which links to https://mui.com/components/buttons/#third-party-routing-library. The last link provides a working example and also explains how this will likely change in react-router v6

like image 150
epsilon Avatar answered Sep 28 '22 21:09

epsilon