From 375f320805a1d19e5793753148c80717a5a818c2 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Fri, 27 Sep 2019 09:52:49 -0700 Subject: [PATCH] Do not pass ref to functional components w/out forwardRef If we can't use forwardRef, we shouldn't ever pass a ref to a functional component. Use innerRef workaround instead. Fixes #6934 --- packages/react-router-dom/modules/Link.js | 19 +++++++++---- packages/react-router-dom/modules/NavLink.js | 30 +++++++++++++------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/packages/react-router-dom/modules/Link.js b/packages/react-router-dom/modules/Link.js index 03614362d7..04ae9839a1 100644 --- a/packages/react-router-dom/modules/Link.js +++ b/packages/react-router-dom/modules/Link.js @@ -5,9 +5,10 @@ import invariant from "tiny-invariant"; import { resolveToLocation, normalizeToLocation } from "./utils/locationUtils"; // React 15 compat +const forwardRefShim = C => C; let { forwardRef } = React; if (typeof forwardRef === "undefined") { - forwardRef = C => C; + forwardRef = forwardRefShim; } function isModifiedEvent(event) { @@ -70,10 +71,8 @@ const Link = forwardRef( ); const href = location ? history.createHref(location) : ""; - - return React.createElement(component, { + const props = { ...rest, - ref: forwardedRef || innerRef, href, navigate() { const location = resolveToLocation(to, context.location); @@ -81,7 +80,17 @@ const Link = forwardRef( method(location); } - }); + }; + + // React 15 compat + if (forwardRefShim !== forwardRef) { + props.ref = forwardedRef || innerRef; + } else { + // TODO: deprecate + props.innerRef = innerRef; + } + + return React.createElement(component, props); }} ); diff --git a/packages/react-router-dom/modules/NavLink.js b/packages/react-router-dom/modules/NavLink.js index c1f98d1fdb..c2561ec457 100644 --- a/packages/react-router-dom/modules/NavLink.js +++ b/packages/react-router-dom/modules/NavLink.js @@ -6,9 +6,10 @@ import Link from "./Link"; import { resolveToLocation, normalizeToLocation } from "./utils/locationUtils"; // React 15 compat +const forwardRefShim = C => C; let { forwardRef } = React; if (typeof forwardRef === "undefined") { - forwardRef = C => C; + forwardRef = forwardRefShim; } function joinClassnames(...classnames) { @@ -67,16 +68,23 @@ const NavLink = forwardRef( : classNameProp; const style = isActive ? { ...styleProp, ...activeStyle } : styleProp; - return ( - - ); + const props = { + "aria-current": (isActive && ariaCurrent) || null, + className, + style, + to: toLocation, + ...rest + }; + + // React 15 compat + if (forwardRefShim !== forwardRef) { + props.ref = forwardedRef || innerRef; + } else { + // TODO: deprecate + props.innerRef = innerRef; + } + + return ; }} );