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
4 Answers
Reset to default 4Yes, 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);