官术网_书友最值得收藏!

HOC composition

The primary reason to create HOCs it to have the ability to compose the features they provide.

Look at the problem from the previous section again. What if we could delegate work to another HOC? For instance, having a mapper HOC called mapPropNames, you can compose it with our previous HOC like this:

makeExpandable(mapPropNames(SomeSection));

Here is the implementation of mapPropNames:

// src/ Chapter_1/ Example_15_HOC_Composition/ App.js

const
mapPropNames = (Component) => (props) => (
<Component
{...props}
isVisible={props.isExpanded}
showHideBox={props.expandOrCollapse}
/>
);

Nice and quick, isn't it? This is a common pattern and is also used when working with backend data sent as JSON. It may adapt the data format to our representation on the frontend layer. As you see, we can employ this great idea when working with HOCs as well!

If you come from an object-oriented background, please notice that the HOC pattern is very similar to the decorator pattern. The decorator, however, also relies on inheritance and needs to implement the interface that it decorates.

Please check  https://en.wikipedia.org/wiki/Decorator_pattern for examples.

You can also compose decorators. It works in a similar way.
主站蜘蛛池模板: 临武县| 普兰县| 平原县| 西和县| 南部县| 盐源县| 定远县| 象山县| 修文县| 松阳县| 西盟| 遵义县| 永济市| 边坝县| 丰城市| 砀山县| 开平市| 柯坪县| 亳州市| 潮安县| 宝鸡市| 扎鲁特旗| 普陀区| 南丰县| 永嘉县| 怀仁县| 当雄县| 游戏| 东城区| 乌审旗| 丹凤县| 玉门市| 玉溪市| 广东省| 登封市| 方山县| 平顺县| 广南县| 乐业县| 文水县| 蒙山县|