June 2019
Intermediate to advanced
192 pages
4h
English
In the abstract, a higher-order component is defined as any function that takes a component and returns another component. Typically, the component returned by the HOC function is a wrapper around the original component that adds functionality. Here’s a simple example:
| ① | const BindProps = (Component, boundProps) => { |
| | const ComponentWithBoundProps = props => ( |
| ② | <Component {...props} {...boundProps} /> |
| | ); |
| | ComponentWithBoundProps.displayName = |
| ③ | `BindProps(${Component.displayName || Component.name})`; |
| | return ComponentWithBoundProps; |
| | }; |
| | |
| ④ | const CarouselWithTestAttr = BindProps(Carousel, { |
| | 'data-test-id': 'carousel', |
| | }); |
The BindProps HOC takes two arguments, a component ...
Read now
Unlock full access