最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct way to hide components in React.js - Stack Overflow

programmeradmin1浏览0评论

Say you are passing a prop called show to a component. If the prop value is true, you should render the full component normally. If it is false, you should not display anything.

You can do this two ways.

  1. return null in the render method of the component.
  2. apply a CSS class containing display: none attribute to the component's DOM element.

Which ones is the correct or the preferred way?

Say you are passing a prop called show to a component. If the prop value is true, you should render the full component normally. If it is false, you should not display anything.

You can do this two ways.

  1. return null in the render method of the component.
  2. apply a CSS class containing display: none attribute to the component's DOM element.

Which ones is the correct or the preferred way?

Share Improve this question asked Jun 15, 2017 at 6:24 Anusha DharmasenaAnusha Dharmasena 1,26914 silver badges25 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

I do not think there will be any definite answer for this question. Each approach has its benefits and drawbacks.

With CSS you have:

  • it might work faster
  • no need to think about restoring child control states if control is shown again.

With returning null:

  • the total rendered DOM might be considerably smaller. This is important if you have many such components that might be hidden.
  • there will be no collisions in rendered html. Lets say you have tabular view. Each tab is its own complex form with many child controls. If you have some raw javascript/jquery/whatever working with their ids/classnames etc. - its quite hard to ensure each tab/form has unique ids, unless you do not render them.

From my point of view the decision will be based upon the structure of your control. If it have complex structure with many nested children and you do not have any means of restoring their states when switched on again - go with CSS, but I would say this is a short term solution for quite simple controls only. In all other cases I would go with not rendering a component.

If you think you would need to display the component again, during that page life, then I would recommend using css way, as the impact on DOM manipulation would be less in that case. In some other cases probably returning null would be more helpful.

For the most part, your two solutions are interchangeable. They both "work" fine.

I would warn against preemptive optimization in choosing which of these methods to choose. If you do need to eventually modify your code and try the other method, this is an absurdly simple swap to make and shouldn't be an obstacle. So don't worry about it until there's a reason to worry about it.

I'm the OP.

If components are hidden depending on the screen size, CSS media queries and display: none works the best if the app is pre-rendered using something like react-snap. This is because, if the pre-rendered device and the viewing device don't match, the layout would change when the app rehydrates if the component hiding logic is in JS.

Related to that, we need to be careful that even though the component is not "shown" with CSS display: none, the component is still there and if there are effects, they will still fire.

发布评论

评论列表(0)

  1. 暂无评论