question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use new forwardRef API in withRouter HOC

See original GitHub issue

React 16.3 is out and now we have an “official” way to pass ref through HOCs with new forwardRef API. Would you like to use it as a replacement for wrappedComponentRef prop to make wrapping transparent for parent components?

Can submit a PR.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

16reactions
mjacksoncommented, Nov 2, 2018

Yes, we should update withRouter to use the React.forwardRef API. As @pshrmn noted above, we can’t do it in 4.x because it is compatible with React 15 and the forwardRef API was introduced in 16.3.

We will fix this in version 5 when we upgrade our dep to React 16.7+.

8reactions
pandaiolocommented, Sep 6, 2019

As suggested in original SO question, here is a version built on @ranjith-s work, a bit simpler and that supports displayName which can be useful in tests or dev tools.

const withRouterAndRef = Wrapped => {
  const WithRouter = withRouter(({ forwardRef, ...otherProps }) => (
    <Wrapped ref={forwardRef} {...otherProps} />
  ))
  const WithRouterAndRef = React.forwardRef((props, ref) => (
    <WithRouter {...props} forwardRef={ref} />
  ))
  const name = Wrapped.displayName || Wrapped.name
  WithRouterAndRef.displayName = `withRouterAndRef(${name})`
  return WithRouterAndRef
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use new forwardRef API in withRouter HOC #6056 - GitHub
React 16.3 is out and now we have an "official" way to pass ref through HOCs with new forwardRef API. Would you like...
Read more >
Forward ref through React Router's withRouter HOC
Based on @Ranjith Kumar answer I came up with the following solution that: Is a bit shorter/simpler (no need for class component or...
Read more >
Developers - Use new forwardRef API in withRouter HOC -
Use new forwardRef API in withRouter HOC. ... React 16.3 is out and now we have an "official" way to pass ref through...
Read more >
Forwarding Refs - React
Forwarding refs in higher-order components​​ The “logProps” HOC passes all props through to the component it wraps, so the rendered output will be...
Read more >
react-router forwardref
forwardRef helps forward a ref through a Higher-order component to reference ... React's new "hooks" APIs give function components the ability to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found