简体   繁体   English

如何在应用程序中构建组件的“组合”?

[英]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 种可能的解决方案:

  1. 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,添加必要的元素和链接

  2. 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/>

  1. Leave only AppHeader component and put elements in it for both users and admins, and just check the "admin" elements and show them if the user is also an admin.只保留 AppHeader 组件并为用户和管理员放入元素,如果用户也是管理员,只需检查“admin”元素并显示它们。

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.

相关问题 如何在Ember应用程序中使用Glimmer组件? - How do I use Glimmer components inside an Ember app? 如何在 Composition API 中构建 IntersectionObserver? - How to build an IntersectionObserver in Composition API? 如何通过组合在 React 组件之间共享钩子 - How to share hooks between React components with composition 如何阅读es6 / javascript中的功能组成? - How do I read functional composition in es6/javascript? 如何从iFrame获取构图和舞台? - How do I get the composition and stage from an iFrame? 如何使用Meteor模板助手功能组合保持干燥? - How do I stay DRY with Meteor template helpers function composition? 如何卸载组件? - How do I unload components? ReactJS和父/子组件:如何构建这个概念性日历? - ReactJS and parent / child components: How do I build out this conceptual calendar? 如何将特定的Webkit构建与我的Cocoa App捆绑在一起 - How do I bundle a specific webkit build with my Cocoa App 如何在我的 Rails 应用资产管道中仅使用 SwiperJS 库的核心组件? - How do I use only core components of the SwiperJS library in my Rails app asset pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM