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.

Connect's wrapped component can't be a stateless function

See original GitHub issue

If the component wrapped with connect is a stateless function component, React warns about the internal ref that the wrapper holds with the following message Warning: Stateless function components cannot be given refs (See ref "wrappedInstance" in App created by Connect(App)). Attempts to access this ref will fail.

import { connect } from 'react-redux';
import React from 'react';

export default connect(mapStateToProps)(props => <div />);

Would it be useful to check whether the component is stateless or not to decide when to apply the ref or do we always need it?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
gaearoncommented, Oct 11, 2015

Yes, you can ignore the warning for now.

1reaction
mini-eggscommented, Mar 21, 2017

I ran into something that may be related.

While using connect with a stateless component that had a child component with state (extends React.Component): the child component’s componentWillReceiveProps would never fire on prop change. Instead the child component with state would rerender and it’s componentDidMount would fire again. I ran into this within a create-react-native-app project.

If this is indeed an issue I would be happy to look into it and create a PR, I could also start a branch on the project to easily reproduce this (I’ve since removed the stateless component with connect).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stateless function components cannot be given refs
I try to access some refs in my component. But I have this error in the console. withRouter.js:44 Warning: Stateless function components cannot...
Read more >
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >
React Functional Components: In-Depth Guide - KnowledgeHut
Stateless functional components do not have state or lifecycle methods. It is easy to make and can easily understand by other developers. Also, ......
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 >
Introduction to Higher-Order Components in React by example
You may want to pass all props to the wrapped component. However, you need to pay attention to refs since they are not...
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