React 16: Stateless function components cannot be given refs
See original GitHub issueI’m using the latest dnd v2.5.4 and still getting a console error for calls to DragLayer
, which is set up in my app as a stateless function component. This is very similar to #896, which concerned DragDropContext
Log:
warning.js:33 Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.
Check the render method of `DragLayer(SitePageCustomDragLayer)`.
in SitePageCustomDragLayer (created by DragLayer(SitePageCustomDragLayer))
in DragLayer(SitePageCustomDragLayer) (created by SitePageContainer)
in div (created by PanelBody)
in PanelBody (created by SitePageContainer)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:19 (1 by maintainers)
Top Results From Across the Web
What does "Stateless function components cannot be given ...
React Redux 3 attaches a ref to the component you give it regardless of whether or not it's stateless. The warning you see...
Read more >Warning: Stateless function components cannot be given refs ...
Stateless functions could not be given refs even in React 15. Try converting your function component into a class component. All ...
Read more >functional components cannot be given refs - You.com | The AI ...
Accepted answer. In React, refs may not be attached to a stateless component. React Redux 3 attaches a ref to the component you...
Read more >What does "Stateless function components cannot be given ...
React Redux 3 attaches a ref to the component you give it regardless of whether or not it's stateless. The warning you see...
Read more >Refs and the DOM - React
This is because a new instance of the function is created with each render, so React needs to clear the old ref and...
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 FreeTop 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
Top GitHub Comments
Still, I hope sometime there will be a fix for this issue, but for now using recompose toClass seems like the easiest way (and easiest to get rid of as soon as the issue is resolved). Borrowing @tough-griff example:
You could wrap your functional component with a div and give a ref to that div