[英]How do I build the “composition” of components in the app?
The app has 2 levels of users, a regular user and an admin.该应用程序有 2 个级别的用户,普通用户和管理员。 You need to make a header component in which some information is the same for everyone, and some - only for admins.
您需要制作一个 header 组件,其中某些信息对每个人都是相同的,而有些信息仅适用于管理员。
There is a basic header component:有一个基本的 header 组件:
<div class="user-header> <img src="logo"> <div class="user-header__links>"... </div> </div>
'user-header__links' contains links, some of which should only be shown to admins. 'user-header__links' 包含链接,其中一些应该只显示给管理员。 Also, the header must still have dropdown, that is available only to admins.
此外,header 必须仍然有下拉菜单,该下拉菜单仅供管理员使用。
I see 3 possible solutions:我看到了 3 种可能的解决方案:
Make 2 different components, ie I have UserHeader for the user, and copy this code to a new component, edit as necessary and get AdminHeader, adding the necessary elements and links制作2个不同的组件,即我有用户的UserHeader,并将此代码复制到一个新组件,根据需要进行编辑并获取AdminHeader,添加必要的元素和链接
in UserHeader, make props / slots for throwing additional content into it, and then adminHeader will look something like this:在 UserHeader 中,制作 props/slots 以将其他内容放入其中,然后 adminHeader 将如下所示:
<UserHeader links={some additional links for admins}> <AdminDropdown/> </UserHeader/>
I understand that in principle all the options are working, but to come to an understanding of which option is more convenient and practical somehow does not work.我知道原则上所有选项都有效,但要了解哪个选项更方便和实用,不知何故是行不通的。 Also, if there are any articles regarding these points, I will be grateful for the links.
另外,如果有任何关于这些要点的文章,我将不胜感激链接。
I'd say that the option 1 is just lots of unnecessary boilerplate, but only you can choose between 2 and 3, as it all depends on your proj structure, implementations and so on...我会说选项 1 只是很多不必要的样板,但只有您可以在 2 和 3 之间进行选择,因为这完全取决于您的项目结构、实现等等......
As with everything, app architecture is a lot about taste... I, personally, very much like the idea of "minimalism" here.与所有事物一样,应用程序架构在很大程度上与品味有关……我个人非常喜欢这里的“极简主义”理念。
I think Conditional Rendering would help you solve this problem - here's React's doc page for it: https://reactjs.org/docs/conditional-rendering.html我认为条件渲染可以帮助您解决这个问题 - 这是 React 的文档页面: https://reactjs.org/docs/conditional-rendering.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.