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.

HOC with wrapped component with asyncConnect

See original GitHub issue

Hi! Is it possible to make HOC which wraps asyncConnect’ed component. I can’t do this. Because all asyncItems, mapStateToProps and mapDispatchToProps of the Component seem to be dropped.

Say, I’ve got the Component:

...
const mapStateToProps = state => ({
    ...
});

const asyncItems = [{
    key: 'myItems',
    promise: ({router, store: { dispatch, getState }}) => {}
}];
const enhancer = createHOC();

module.exports = enhancer(asyncConnect(
    asyncItems,
    mapStateToProps,
    mapDispatchToProps,
)(Component));

And HOC itself has the asyncConnect enhancer 😃 Is it possible or do I have to abandon this idea at all?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gerhardslettencommented, Jul 18, 2018

After upgrading to RR v4 and the latest version of redux-connect, I think that the solution for both data-fetching and code-splitting is about organizing you code. The route-level component can’t both be HOC’ed with redex-connect and be lazy-loaded, so you should do data-fetching in the route-level component and move lazy-loading into a child-component. Can’t think of any other way to do this, since you don’t have a getComponent function with react-router-config, without writing you own helper that both lazy-load and do data-fetching. Maybe this should be mentions in the README.md to save people from spending time looking into this?

1reaction
gerhardslettencommented, Aug 16, 2018

Hi, this is an example of a controller. You just move the visuals and heavy dependencies into its own component, and just hot-load it from the controller, but keep the asyncConnect hoc on the controller so redux-connect will discover it through react-router-config

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { asyncConnect } from 'redux-connect'
import loadable from 'loadable-components'

import { isLoaded as isPageLoaded, load as loadPage } from 'redux/modules/page'

const Page = loadable(() =>
  import(/* webpackChunkName: "page" */'./Page')
)

@asyncConnect([
  {
    promise: ({ store: { dispatch, getState }, location: { pathname } }) => {
      const promises = []
      if (!isPageLoaded(getState(), pathname)) {
        promises.push(dispatch(loadPage(pathname)))
      }
      return Promise.all(promises)
    }
  }
])
@connect(state => ({
  page: state.page.data,
  error: state.page.error
}))
export default class PageContainer extends Component {
  render() {
    return <Page {...this.props} />
  }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Higher-Order Components - React
A HOC is a pure function with zero side-effects. And that's it! The wrapped component receives all the props of the container, along...
Read more >
How to Wrap One React Component into Another | Pluralsight
The HOC function outputs a class component with improvements we want to make, like additional lifecycle methods, API endpoints, etc. render() ...
Read more >
Adding props to wrapped component with HOC - Stack Overflow
As I know, HOC will be triggered once it's initiated to render. At that time your props passed through HOC are available.
Read more >
Introduction to Higher-Order Components in React by example
We have created a new component called HOC which returns the <WrappedComponent/> from its render function. While this actually adds no ...
Read more >
react redux use hoc with connected component - splunktool
Is it possible to make HOC which wraps asyncConnect'ed component. I can't do this. Because all asyncItems, mapStateToProps 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