Connect's wrapped component can't be a stateless function
See original GitHub issueIf 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:
- Created 8 years ago
- Comments:9 (5 by maintainers)
Top 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 >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
Yes, you can ignore the warning for now.
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’scomponentDidMount
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).