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.

React 16: Stateless function components cannot be given refs

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
bknifflercommented, Dec 22, 2017

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:

import { toClass } from 'recompose';

const wrapInTestContext = (WrappedComponent) => {
  const TestContextWrapper = toClass(props => (
    <Provider store={store}>
      <StaticRouter context={context}>
        <WrappedComponent {...props} />
      </StaticRouter>
    </Provider>
  ));

  return DragDropContext(TestBackend)(TestContextWrapper);
};
3reactions
MadeoITcommented, Jul 4, 2018

You could wrap your functional component with a div and give a ref to that div

Read more comments on GitHub >

github_iconTop 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 >

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