简体   繁体   English

React shouldComponentUpdate()不会阻止重新渲染

[英]React shouldComponentUpdate() does not prevent re-render

I'm using the ThreeDisplay React component shown below for holding a WebGL/Three.js canvas (the canvas itself is not part of the component, it gets appended to the container div by an initialization script). 我正在使用下面显示的ThreeDisplay React组件来保存WebGL / Three.js画布(画布本身不是该组件的一部分,它会通过初始化脚本附加到容器div )。

I want the component only to update after every RUN and UPDATE action. 我只希望组件在每个RUNUPDATE操作之后进行更新。 Those actions get dispatched by the parent component of ThreeDisplay . 这些动作由ThreeDisplay的父组件ThreeDisplay

Now for some reason, the component also gets updated/re-rendered if the last action was FADE_COLOR or SWITCH_COLOR . 现在由于某种原因,如果最后一个动作是FADE_COLORSWITCH_COLOR ,则组件也将得到更新/重新渲染。 Those actions get dispatched by ThreeDisplay, they are triggered by mouse events, as shown in the code below. 这些操作由ThreeDisplay分派,它们由鼠标事件触发,如下面的代码所示。

I'm trying to use shouldComponentUpdate() to update only after the aforementioned actions. 我试图仅在上述操作之后使用shouldComponentUpdate()进行更新。 But for some reason, this does not prevent the component to re-render on every mouse event. 但是由于某些原因,这不会阻止在每次鼠标事件时重新渲染组件。

The full code of my application/prototype can be found in this repository 我的应用程序/原型的完整代码可以在此存储库中找到

    import React from 'react'
    import {connect} from 'react-redux'

    import {fadeColor, switchColor} from '../actions'

    class ThreeDisplay extends React.Component {
        shouldComponentUpdate(nextProps) {
            const shouldUpdate =
                nextProps.lastAction === 'RUN' || nextProps.lastAction === 'UPDATE'

            if (!shouldUpdate) {
                console.log('ThreeDisplay will not update on ' + nextProps.lastAction)
            }

            return shouldUpdate
        }

        componentWillUpdate() {
            // This gets logged even if lastAction ist not 'RUN' or 'UPDATE'
            console.log('ThreeDisplay will update on ' + this.props.lastAction)
        }

        render() {
            return (
                <div
                    id="container"
                    className={
                        this.props.running
                            ? 'three-display'
                            : 'three-display hidden'
                    }
                    onClick={this.props.switchColor}
                    onMouseMove={this.props.fadeColor}
                />
            )
        }
    }

    const mapStateToProps = state => {
        return {
            running: state.running,
            lastAction: state.lastAction
        }
    }

    const mapDispatchTopProps = dispatch => {
        return {
            fadeColor: e => dispatch(fadeColor(e)),
            switchColor: () => dispatch(switchColor())
        }
    }

    export default connect(mapStateToProps, mapDispatchTopProps)(ThreeDisplay)

In this expression 在这个表达中

const shouldUpdate = nextProps.lastAction === 'RUN' || 'UPDATE'

If nextProps.lastAction === 'RUN' is false, then the code evaluates the other branch of the OR, ie just the 'UPDATE' string, which is always true, thus shouldUpdate will always be true. 如果nextProps.lastAction === 'RUN'为假,则代码将评估OR的另一个分支,即仅是'UPDATE'字符串,该字符串始终为true,因此shouldUpdate将始终为true。

Replace it with 替换为

const shouldUpdate = nextProps.lastAction === 'RUN'
  || nextProps.lastAction ===  'UPDATE'

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

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