简体   繁体   English

在没有 setState 的情况下反应推送到状态变量

[英]React push to state variable without setState

Lately I've been working on a React app.最近我一直在研究 React 应用程序。

In one of the pages which was suffering from slow load, I discovered there was some updates to state, and those variables where not meant to be shown to end user, and they were there to be checked later when some event was raised.在其中一个加载缓慢的页面中,我发现有一些状态更新,以及那些不打算向最终用户显示的变量,并且稍后在引发某些事件时检查它们。

Since those were arrays, I mutating the state directly and event without "setState"ing it (I know mutating the variable is not recommended at all!).因为那些是数组,所以我直接改变状态和事件而不“设置状态”(我知道根本不推荐改变变量!)。 Later when the Event was raised, state variable had correct data and since redundant "setState"s where removed, the page had a significant speed boost and the problem was solved.后来当事件被引发时,状态变量有正确的数据,并且由于删除了冗余的“setState”,页面速度有了显着的提升,问题得到了解决。

Anyone has any similar experiences with "setState" in react?任何人都对“setState”有类似的反应? What do you think about this?你怎么看待这件事?

...I discovered there was some updates to state, and those variables where not meant to be shown to end user... ...我发现状态有一些更新,而那些不打算向最终用户显示的变量......

Then they shouldn't be part of state, they should just be properties on the instance (in a class-based component, which I assume you're using; variables you close over in a functional component).那么它们不应该是状态的一部分,它们应该只是实例上的属性(在基于类的组件中,我假设您正在使用它;您在功能组件中关闭的变量)。 state should only contain things that, when changed, cause a re-render. state应该只包含更改时会导致重新渲染的内容。 For that reason, you should only change them via setState , so the render is triggered.出于这个原因,您应该只通过setState更改它们,以便触发渲染。 You're getting away with breaking the rule because what you're changing isn't related to rendering.您违反了规则,因为您正在更改的内容与渲染无关。

Never mutate the state object (or any objects it refers to) directly.永远不要直接改变state对象(或它引用的任何对象)。 But you can mutate non-state properties (or things you close over), that's fine.但是你可以改变非状态属性(或者你关闭的东西),这很好。

If you need those variables to be stored in actual state but them does not have to trigger a render you probably need to implement shouldComponentUpdate lyfecycle callback:如果您需要将这些变量存储在实际状态但它们不必触发渲染,您可能需要实现shouldComponentUpdate lyfecycle 回调:

shouldComponentUpdate(nextProps, nextState) {
        return nextState.myPropertyThatTriggersRender != this.state.myPropertyThatTriggersRender;
    }

Doing that, if all your state's properties that trigger render are checked, other state properties won't trigger a render .这样做,如果所有触发渲染的状态属性都被检查,其他状态属性将不会触发渲染 But will be still available in state object .但仍将在 state object 中可用

In order to improve performances, you can implement the same check on the child elements (if they are components) that are going to be rendered.为了提高性能,您可以对将要渲染的子元素(如果它们是组件)实施相同的检查。

shouldComponentUpdate(nextProps, nextState) {
        return nextProps.myPropThatTriggersRender != this.props.myPropThatTriggersRender;
    }

From React's DOC:来自 React 的文档:

Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props.使用 shouldComponentUpdate() 让 React 知道组件的输出是否不受当前 state 或 props 变化的影响。 The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.默认行为是在每次状态更改时重新渲染,在绝大多数情况下,您应该依赖默认行为。

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

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