简体   繁体   English

基于用户的屏幕打开模式(即查看、编辑、创建)在反应应用程序中显示/隐藏、启用/禁用组件的更好方法是什么

[英]What are better ways to show/hide, enable/disable components in react applications based on user's screen open mode i-e view, edit, create

I am working on a react application and I want code design related suggestions and feedback like what can be better ways to handle following work.我正在开发一个反应应用程序,我想要与代码设计相关的建议和反馈,比如什么可以更好地处理后续工作。

I have a react application in which I want to hide/show, disable/enable html elements based on user's screen open mode.我有一个反应应用程序,我想根据用户的屏幕打开模式隐藏/显示、禁用/启用 html 元素。 For example: My application has data filters which I have to hide when user open mode is view but want to show them when open mode is edit or create .例如:我的应用程序有数据过滤器,当用户打开模式为view时我必须隐藏这些过滤器,但想在打开模式为editcreate时显示它们。 Similarly I want to disable textFields when screen open mode is view and enable them when mode is edit or create .同样,我想在屏幕打开模式为view时禁用 textFields,并在模式为editcreate时启用它们。

Now I have implemented above work like this, in which children are shown when mode is not view.现在我已经像这样实现了上面的工作,其中当模式不是视图时会显示孩子。

My questions are:我的问题是:

  1. Is this a correct approach used to show and hide elements based on screen open mode??这是基于屏幕打开模式显示和隐藏元素的正确方法吗?
  2. Some elements I want to disable but not hide ie like textFields should show only label, comboboxes elements should be disabled but not hidden.我想禁用但不隐藏的一些元素,例如 textFields 应该只显示 label,组合框元素应该禁用但不隐藏。 What can be ways to achieve this, other than disabling each individual component with disabled prop.除了使用 disabled 属性禁用每个单独的组件之外,还有什么方法可以实现这一点。

Any help will be appreciated.任何帮助将不胜感激。

interface Props {
  mode?: 'view' | 'edit' | 'create',
  children: React.ReactElement,
}

const AccessControl = (props: Props) => {
  if (props.mode !== "view") {
    return props.children;
  }

  return null;
}

export default AccessControl;

<AccessControl mode="view">
   <TextField
     name="name"
     value={this.state.name}
     id="name"
   />
</AccessControl>

Thank you.谢谢你。

I would use a display: (mode==='view')? 'block' | 'none'我会使用display: (mode==='view')? 'block' | 'none' display: (mode==='view')? 'block' | 'none' display: (mode==='view')? 'block' | 'none' to implement the same idea, because it is much faster for the browser to show or hide an element than for you to load in something new. display: (mode==='view')? 'block' | 'none'来实现相同的想法,因为浏览器显示或隐藏元素比加载新元素要快得多。

Say if you had an external resource in a div that is not cached, every time you are switching modes, you are adding a new element into the DOM and the browser will fire off a request to obtain some data.假设您在 div 中有一个未缓存的外部资源,每次切换模式时,您都在向 DOM 添加一个新元素,浏览器将触发获取一些数据的请求。 Hiding a div means you don't have to call this external resource again.隐藏 div 意味着您不必再次调用此外部资源。

With disabling - there's no 'disable' attribute to elements, so you'll have to pass something through to the component for it to implement.禁用 - 元素没有“禁用”属性,因此您必须将某些内容传递给组件才能实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM