javascript - 如何在reactjs中停止重新渲染?

标签 javascript reactjs render rerender

 case 'splitWord': 
            if(this.props.remedStatus){
                console.log(this.props.remedStatus)
                console.log("remed")
                return(
                   <HotspotRemed 
                        xml={this.receivedXML}
                        userXml={this.usersAnswerXML}
                        correctAnswer ={this.ansString}
                        type={this.splitType}
                    />
                ) 
            } else {
                console.log("reurn word")
                return(
                    
                    <div style ={wrap_style} >
                    {this.forceUpdate}
                        <SplitXMLPreview xml={this.receivedXML}
                            type='w' 
                            assignedSplitContentClick={(contentId, selectionType) => {
                                this.handleSplitContentClick(contentId, selectionType, 'w')
                            }}
                            getCorrectAns={(answers) => {
                                this.correctAnswers = answers
                            }}
                        />
                    </div>
                )
            }
           
            break
            
            
            <<<==NOW HERE IS MY RENDER MEHTOD.==>>>
            
                render() {
        console.log("render")
            return (
                <div className="previewtemplate" style ={template}>
                    {this.templateArea(this.state.templateType)}
                </div>
            );
        
    }
}
            
            

我只是想知道如何停止重新渲染组件。

我有三个按钮,当我单击一个按钮时,它会执行该操作。

只有一个按钮。其中我使用 if else 当我单击该按钮时它返回 false 和组件 false 调用,这很好工作,当再次单击该按钮时它调用 true 组件,这对我来说也很棒,但是当我再次单击按钮时首先它调用渲染然后调用我的组件。我不想调用渲染它调用我的组件。

我读了一些博客,他们建议使用 this.forceupdate。 我该怎么办?帮帮我。

最佳答案

您可以实现自定义逻辑,以确定组件是否应使用 shouldComponentUpdate 方法更新(重新渲染)。

例如:

shouldComponentUpdate () {
    return false // this will lead to never re-render the component
}

Note that in the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.

Here is a deeper explanation来自文档。

关于javascript - 如何在reactjs中停止重新渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48710062/

相关文章:

php - 语法错误 T_PAAMAYIM_NEKUDOTAYIM!

javascript - 如何为 amChart 设置 x 轴标题和 y 轴名称?

javascript - 我可以用 json 渲染图像吗?

reactjs - 如何在单页reactjs/electron应用程序中搜索文本

javascript - 在ES6类React组件的render方法中使用返回JSX的React组件函数报错

ruby-on-rails - 具有 Controller 条件的 Rails 布局渲染

javascript - 在div内执行javascript

javascript - 如何使用 webgl 和 glMatrix 围绕世界轴旋转对象

javascript - Push 函数改变了 React 中的 props

reactjs - ESLint 损坏 : Rules with suggestions must set the `meta.hasSuggestions` property to `true`