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

Sub-rendering

It is worth stressing that we always want to keep our components very small and our render methods very clean and simple.

However, that is not an easy goal, especially when you are creating an application iteratively, and in the first iteration you are not sure exactly how to split the components into smaller ones. So, what should we be doing when the render method becomes too big to maintain? One solution is to split it into smaller functions in a way that lets us keep all the logic in the same component.

Let's look at an example:

  renderUserMenu() { 
// JSX for user menu
}

renderAdminMenu() {
// JSX for admin menu
}

render() {
return (
<div>
<h1>Welcome back!</h1>
{this.userExists && this.renderUserMenu()}
{this.userIsAdmin && this.renderAdminMenu()}
</div>
);
}

This is not always considered best practice because it seems more obvious to split the component into smaller ones. However, sometimes it helps to keep the render method cleaner. For example, in the Redux real-world examples, a sub-render method is used to render the load more button.

Now that we are JSX power users, it is time to move on and see how to follow a style guide within our code to make it consistent.

主站蜘蛛池模板: 荥经县| 惠水县| 枞阳县| 东方市| 崇文区| 鄂尔多斯市| 萨迦县| 金秀| 水城县| 瑞昌市| 土默特右旗| 宜昌市| 泰顺县| 区。| 兴业县| 溆浦县| 罗源县| 西盟| 乌审旗| 聂拉木县| 绥宁县| 巴林右旗| 绥阳县| 曲沃县| 沙坪坝区| 泊头市| 阳谷县| 聊城市| 星子县| 福清市| 彭泽县| 连云港市| 丰顺县| 鹿泉市| 苏州市| 大丰市| 武陟县| 延庆县| 剑川县| 武邑县| 甘德县|