Get class based components REF with lazy import
See original GitHub issueDo 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:
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s working 😃
Try react-hot-loader@4.8.3