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.

Use connect on HOC compoennts

See original GitHub issue

Is there any way one could use connect on HOCs and fetch a data from store?

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

function withCurrentUser(Component) {
  return function(...props) {
    return <Component currentUser={/* Pass current user fetched from store here */} {...props} />
  }
}

function mapStateToProps(state) {
  return {
    currentUser: state.user
  }
}

export default connect(mapStateToProps)(withCurrentUser);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

44reactions
markeriksoncommented, Dec 9, 2017

Not written that way, since connect expects an actual component type as the second function’s parameter. However, this should work:

import {compose} from "redux";
import {connect} from "react-redux";

const composedHoc = compose(
    connect(mapStateToProps),
    withCurrentUser
);

export default composedHoc;

At least, I think that will work. I know composing HOCs works in general, just not sure on the exact syntax. Hopefully that at least points you in a useful direction.

28reactions
foadkcommented, Oct 8, 2018

It’s a bit old, but you could achieve this with a little modification of your code:

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

function withCurrentUser(Component) {
  function yourFunction(...props) {
    return <Component currentUser={/* Pass current user fetched from store here */} {...props} />
  }
  return connect(mapStateToProps)(yourFunction);
}

function mapStateToProps(state) {
  return {
    currentUser: state.user
  }
}

export default withCurrentUser;
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to connect a HOC with React and Redux - Level Up Coding
Create a loading component using HOCs for fetching data from your server and have fun! ; Fetch all data from your server API...
Read more >
React Redux use HOC with connected component
Here is a simple example how it works const AppWrapper = MainComponent => class extends Component{ componentWillmount(){ this.props.
Read more >
Connect | React Redux
The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it...
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 >
Connected Higher Order Component (HOC) | by Karan Jariwala
One way to go about doing this is to Connect the components with the Redux Store and then to add appropriate disable or...
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