Skip to content

Commit

Permalink
React 15 compatibility remix-run#6954
Browse files Browse the repository at this point in the history
  • Loading branch information
chitezh committed Oct 25, 2019
1 parent 8c5ce32 commit d91ef38
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/react-router/modules/withRouter.js
Expand Up @@ -4,9 +4,12 @@ import hoistStatics from "hoist-non-react-statics";
import invariant from "tiny-invariant";

import RouterContext from "./RouterContext.js";

// React 15 compat
let { forwardRef } = React;
const forwardRefShim = C => C;
if (typeof forwardRef === "undefined") {
forwardRef = C => C;
forwardRef = forwardRefShim;
}

/**
Expand All @@ -17,20 +20,20 @@ function withRouter(Component) {
const C = forwardRef((props, forwardedRef) => {
const { wrappedComponentRef, ...remainingProps } = props;

// React 15 compat
let ref = wrappedComponentRef;
if (forwardRefShim !== forwardRef && forwardedRef) {
ref = forwardedRef;
}

return (
<RouterContext.Consumer>
{context => {
invariant(
context,
`You should not use <${displayName} /> outside a <Router>`
);
return (
<Component
{...remainingProps}
{...context}
ref={forwardedRef || wrappedComponentRef}
/>
);
return <Component {...remainingProps} {...context} ref={ref} />;
}}
</RouterContext.Consumer>
);
Expand Down

0 comments on commit d91ef38

Please sign in to comment.