简体   繁体   English

如何停止reactjs中的重新渲染?

[英]how to stop re-render in reactjs?

  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> ); } } 

I just simply want that how to stop re-render the component. 我只是想知道如何停止重新渲染组件。

I've three button when i click on one button it perform there action. 当我单击一个按钮时,我有三个按钮,它在那里执行操作。

There is one button. 有一个按钮。 in which i use if else when i click on that button it return false and component false call that is great work fine and when in click on again that button it call true component that is also great work for me but when i click again on button first it call render then in call my component. 在我使用if的情况下,当我单击该按钮时,它返回false和component false调用,这很好用,当再次单击该按钮时,它调用true组件,这对我来说也很好,但是当我再次单击按钮时首先调用渲染,然后调用我的组件。 i want not call render it call my compoenent. 我不想将其称为渲染。

i read some blog they suggest use this.forceupdate. 我读了一些博客,他们建议使用this.forceupdate。 what should i do?Help me. 我该怎么办?

You can implement custom logic that determines if the component should update (re-render) with the shouldComponentUpdate method. 您可以实现自定义逻辑,该逻辑确定组件是否应使用shouldComponentUpdate方法进行更新(重新渲染)。

eg : 例如:

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. 注意,将来React可能会将shouldComponentUpdate()视为提示而不是严格的指令,并且返回false仍可能导致组件的重新呈现。

Here is a deeper explanation from the documentation. 这是文档中的更详细说明

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

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