- React Design Patterns and Best Practices(Second Edition)
- Carlos Santana Roldán
- 208字
- 2021-06-24 15:43:40
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.
- 物聯網安全(原書第2版)
- 網管員典藏書架:網絡管理與運維實戰寶典
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 智慧光網絡:關鍵技術、應用實踐和未來演進
- 基于性能的保障理論與方法
- 端到端QoS網絡設計
- 網絡利他行為研究:積極心理學的視角
- 無線傳感器網絡定位技術
- 大型企業微服務架構實踐與運營
- Hands-On Docker for Microservices with Python
- Guide to NoSQL with Azure Cosmos DB
- 數字王國里的虛擬人:技術、商業與法律解讀
- SEO攻略:搜索引擎優化策略與實戰案例詳解
- Hands-On Cloud:Native Microservices with Jakarta EE
- 5G新型多址技術