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.

Get class based components REF with lazy import

See original GitHub issue

Do you want to request a feature or report a bug?

  • Feature request

What is the current behavior?

  • Can’t get components REF.

Issue is:

I have ComplexGrid that extends React.PureComponent. I want to load this component dynamically.

React version = 16.8.5 Eg:

const ComplexGrid = React.lazy(() => import('@cc/grid/ComplexGrid'));

Then inside constructor I create this.gridRef = React.createRef();

Then JSX part:

<React.Suspense fallback={<div>Loading</div>}> <ComplexGrid ref={this.gridRef} {...some props here} /> </React.Suspense>

And then I get warning about React.forwardRef. The problem:

  • My ComplexGrid component has some methods (UpdateColumnMetrics, …etc)
  • I need ref to call these methods.
  • I don’t need to forward ref to ‘div’ element (or others), I need component’s instance
  • I need normal ref behavior, like with static imports

Is here any solution?

codesandbox example:

https://codesandbox.io/embed/ojkk2z5335

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
igoriok1994commented, Apr 8, 2019

Try react-hot-loader@4.8.3

It’s working 😃

1reaction
theKasheycommented, Apr 5, 2019

Try react-hot-loader@4.8.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible to forward a ref using React.Suspense and ...
Setting the ref directly on a Lazily loaded component always returns null. It would be nice if it returned the value from React.createRef()....
Read more >
Code-Splitting
The React.lazy function lets you render a dynamic import as a regular component. Before: import OtherComponent from './OtherComponent';.
Read more >
How to set up lazy loading components in React
React.lazy function takes a promise based function and returns it. ... Disadvantage: You must import all page components one by one manually ...
Read more >
How To Manage State on React Class Components
In this tutorial, you'll run through an example of managing state on class-based components. As of the writing of this tutorial, ...
Read more >
Getting started with standalone components
imports can also be used to reference standalone directives and pipes. In this way, standalone components can be written without the need to...
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