简体   繁体   English

React 渲染所有组件,同时使用 json 作为 state

[英]React renders all component while using a json as state

Hiho,嗨,

I'm using react for a bigger project, but I'm still a beginner there.我正在将 react 用于一个更大的项目,但我仍然是那里的初学者。 Currently I have performance issues while entering information into a textfield on a big form page.目前,我在大型表单页面上的文本字段中输入信息时遇到性能问题。 I recognized that it is caused by the state set into the value property which is connected with all ui elements.我认识到这是由与所有 ui 元素连接的 value 属性中设置的 state 引起的。 I have a structure similar to:我的结构类似于:

<textfield value={myState.name} ...>...</textfield>
<textfield value={myState.someotherattr} ...>...</textfield>
...
<datePicker value={myState.date} ...>...</datePicker>
...

The state is synced by the database (automatically) and always contains the current information from the database. state 由数据库同步(自动),并始终包含来自数据库的当前信息。 Always when I change a value in a textfield (by onChange and setMyState((oldState => {...oldState, name: newValue}))) all components related to that json will re-render.当我更改文本字段中的值时(通过 onChange 和 setMyState((oldState => {...oldState, name: newValue}))),与 json 相关的所有组件都会重新渲染。 That leads to a huge input lag while typing.这会导致打字时出现巨大的输入延迟。

I built a small example to reproduce this behavior.我建立了一个小例子来重现这种行为。 Here are 2 counters which will be increased by a button.这里有 2 个计数器,将通过一个按钮增加。 I would assume, that only the increased React Component will re-render.我会假设,只有增加的 React 组件才会重新渲染。 But if I use the JSON variant both re-renders:但是,如果我使用 JSON 变体,则会重新渲染:

JSON variant: JSON 变体:

https://github.com/Tockra/react-mini-example/blob/with-json/pages/index.tsx https://github.com/Tockra/react-mini-example/blob/with-json/pages/index.tsx

Here the flamegraph which shows both components re-rendering:这里显示两个组件重新渲染的火焰图: 替代文字

Seperate state variant:单独的 state 变体:

https://github.com/Tockra/react-mini-example/blob/with-seperate/pages/index.tsx https://github.com/Tockra/react-mini-example/blob/with-seperate/pages/index.tsx

Here the flamegraph which shows both components re-rendering:这里显示两个组件重新渲染的火焰图: 替代文字

Is there any way to prevent the re-rendering without extracting all JSON properties to own react states with own setters?有没有办法在不提取所有 JSON 属性的情况下防止重新渲染以使用自己的设置器来拥有反应状态? It would be very annoying for my real page with 10-12 input fields.对于具有 10-12 个输入字段的真实页面来说,这将是非常烦人的。

T

In general, in react when you have:一般来说,当你有反应时:

<Parent>
 <Child1/>
 <Child2/>
</Parent>

If you rerender the parent (say because you change state there), all children will re render.如果您重新渲染父级(比如说因为您在那里更改了 state),所有子级都将重新渲染。

If you want to prevent that look into React.memo , that will prevent children from rerendering if their props didn't change .如果你想阻止对React.memo的查看,如果他们的 props没有改变,这将阻止孩子重新渲染。

Also, it could be you have stored state of a child in parent, and hence when child updates state in parent all children rerender.此外,您可能已经在父级中存储了子级的 state,因此当子级在父级中更新 state 时,所有子级都会重新渲染。 But in some situations it maybe also possible to move that state from parent to child, in that case, if child changes state (which doesn't live in parent anymore), other children won't re render.但在某些情况下,也可以将 state 从父级移动到子级,在这种情况下,如果子级更改 state(不再存在于父级中),其他子级将不会重新渲染。 But it depends, sometimes it maybe more desirable to have child state in parent.但这取决于,有时在父母中拥有孩子 state 可能更可取。

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

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