Renée Ghattas
Software Developer @ Qlik
A higher-order component is a function that takes a component and returns a new component.
// Take in a component as argument WrappedComponent
function simpleHOC(WrappedComponent) {
// And return a new anonymous component
return class extends React.Component{
render() {
return <WrappedComponent {...this.props}/>;
}
}
}
Basic example:
Going back to this...
export default connect(mapStateToProps, mapDispatchToProps)(MyFancyComponent)
Redux: connect HOC
The connect HOC connects a component to the Redux store.
<withRedux>
<withAuth>
<withLogging>
<withLayout>
<MyFancyComponent />
</withLayout>
</withLogging>
</withAuth>
</withRedux>
import React from 'react';
const SECRET_TO_LIFE = 42;
class ShareSecretToLife extends React.Component {
render() {
return (
<div>
{this.props.render({ secretToLife: SECRET_TO_LIFE })}
</div>
);
}
}
const ShareSecretWithWorld = () => (
<ShareSecretToLife
render={({ secretToLife }) => (
<h1>{secretToLife}</h1>
)}
/>
);
🤔
Forget everything you've learned about React...
Lol jk.
Sounds good?
const page = compose(
withRedux,
withAuth,
withLogging,
withLayout('default'),
);
Going back to this...
import { useSelector, useDispatch } from 'react-redux'
Instead of using connect() HOC...
export default connect(mapStateToProps, mapDispatchToProps)(MyFancyComponent)