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.

Stateless function components cannot be given refs. Attempts to access this ref will fail

See original GitHub issue

Hello,

I’m having an issue with react-select, when trying to use a custom Option component.

This is what my option component looks like:

import * as React from 'react';

const Option = (props) => {
  const handleMouseDown = (event) => {
    event.preventDefault();
    event.stopPropagation();
    props.onSelect(props.option, event);
  };
  const handleMouseEnter = (event) => {
    props.onFocus(props.option, event);
  };
  const handleMouseMove = (event) => {
    if (props.isFocused) return;
    props.onFocus(props.option, event);
  };

  return (
    <div className={props.className}
         onMouseDown={handleMouseDown}
         onMouseEnter={handleMouseEnter}
         onMouseMove={handleMouseMove}
         title={props.option.title}>
      <img src={props.option.thumbnail}/> {props.children}
    </div>
  );
};

export default Option;

And used on the Select element like optionComponent={Option}.

However, with this, I’m getting the following error:

index.js:2177 Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

Check the render method of `Select`.
    in Option (created by Select)
    in div (created by Select)
    in div (created by Select)
    in div (created by Select)

Did I miss something?

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

4reactions
nWidartcommented, Mar 27, 2018

Turns out the solution was to implement the OptionComponentProps for the props and convert to a react class.

class Option extends React.Component<OptionComponentProps> {
0reactions
taven-liucommented, Jun 25, 2018

this code work normal

export class Options extends React.PureComponent {
   ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "Stateless function components cannot be given ...
Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail. Check the render method of DragSource( ...
Read more >
Solved: Function components cannot be given refs ... - GitHub
Solved: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? #2120.
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 >
warning: function components cannot be given refs. attempts ...
As the warning state you cannot assign refs to functional component without the usage the forwardRef ... given refs. Attempts to access this...
Read more >
gpbl/react-day-picker - Gitter
I got this Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.
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