热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

渲染所有可能的元素或根据请求渲染-Renderallpossibleelementsorrenderonrequest

SoIhaveanappthathasarightsidebarwhosevisibilityistoggledviaabutton.Inthatsidebar

So I have an app that has a right sidebar whose visibility is toggled via a button. In that sidebar there can be one of several things [at a time] - chat, help, search. I was looking at some plain HTML from apps which have a similar feature and noticed that they have all nodes rendered, but are just hidden via CSS.

所以我有一个带有右侧边栏的应用程序,其可见性通过按钮切换。在那个侧边栏中,[一次]可以有几个东西中的一个 - 聊天,帮助,搜索。我正在从具有类似功能的应用程序中查看一些简单的HTML,并注意到它们已经渲染了所有节点,但只是通过CSS隐藏。

Since I need to do the same thing, I was thinking whether this would be a good idea to do with React. But then I realized that React elements have a state which when updated calls the render method. So I can use the state to store both whether the sidebar is opened, and what is inside the sidebar.

由于我需要做同样的事情,我在想是否这对React来说是个好主意。但后来我意识到React元素有一个状态,当更新时调用render方法。因此,我可以使用状态来存储侧边栏是否打开以及侧边栏内部的内容。

Is that the React way of doing things? Is it better to have all nodes rendered even if they are not visible, or is it better to have the nodes rendered on request via state changes?

这是React的做事方式吗?是否更好地渲染所有节点,即使它们不可见,或者通过状态更改请求呈现节点更好?

1 个解决方案

#1


0  

My feeling is that only rendering what is visible would be the more standard React way, but in this case, this is mainly a performance decision. If you render everything and just toggle visibility with CSS, the first render will take longer (but the time difference may not be relevant or even noticeable). If you render only the part that's visible, React needs to do a small rerender every time the sidebar content changes. (This may also not be noticeable time.)

我的感觉是,只渲染可见的内容将是更标准的React方式,但在这种情况下,这主要是一个性能决策。如果渲染所有内容并仅使用CSS切换可见性,则第一次渲染将花费更长时间(但时差可能不相关甚至不明显)。如果只渲染可见的部分,每当侧边栏内容发生变化时,React都需要做一个小的重新渲染。 (这可能也不是明显的时间。)

My recommendation would be to try both, if you want to test the performance. But I think you won't go too wrong either way.

如果你想测试性能,我的建议是尝试两者。但我认为你不会走错路。


推荐阅读
author-avatar
胡宸宸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有