javascript - How to combine Higher Order Components? - Stack Overflow

How can I cleanly bine Higher Order Components? Currently I have something like this, and there has to

How can I cleanly bine Higher Order Components? Currently I have something like this, and there has to be a cleaner way to bine and ponse HOC's

const appWithWidth = withWidth()(App);

const appWithReduxAndWidth = connect(mapStateToProps, mapDispatchToProps)(appWithWidth);

const appWithRouterAndCookiesAndReduxAndWidth = withRouter(withCookies(appWithReduxAndWidth));

export default appWithRouterAndCookiesAndReduxAndWidth;

How can I cleanly bine Higher Order Components? Currently I have something like this, and there has to be a cleaner way to bine and ponse HOC's

const appWithWidth = withWidth()(App);

const appWithReduxAndWidth = connect(mapStateToProps, mapDispatchToProps)(appWithWidth);

const appWithRouterAndCookiesAndReduxAndWidth = withRouter(withCookies(appWithReduxAndWidth));

export default appWithRouterAndCookiesAndReduxAndWidth;
Share Improve this question asked Nov 28, 2017 at 14:53 Sua MoralesSua Morales 91611 silver badges22 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Yes, repose is a great library for this.

For example, use pose:

export default pose(
    withWidth(all_the_cookie_width), 
    withCookies, 
    withMilk, 
    withHoldTheSprinkles
)(App);

You can use pose from repose which bines multiple higher-order ponents.

import { pose } from 'repose'

export default pose(
  withWidth,
  connect(mapStateToProps, mapDispatchToProps),
  withCookies,
  withRouter
)(App)

You can use the pose included with redux:

import { pose } from 'redux';

export default pose(
  withRouter,
  withCookies
  connect(mapStateToProps, mapDispatchToProps),
  withWidth()
)(App);

For me the pose worked, but the order matters as per my testing. When I used withRouter, withWidth from material-ui, it was showing an error of 'setState in infinite loop'. Not sure about the reason, probably some can explain. Here is my code and will help someone.

import pose from 'repose/pose';
....
...
....
export default pose(
  connect(mapStateToProps, null),
    withRouter,
  withWidth()
)(AppNav);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744240100a4564665.html

相关推荐

  • javascript - How to combine Higher Order Components? - Stack Overflow

    How can I cleanly bine Higher Order Components? Currently I have something like this, and there has to

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信