0

 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 的博客。我该怎么办?帮帮我。

4

1 回答 1

3

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

例如:

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

请注意,将来 React 可能会将 shouldComponentUpdate() 视为提示而不是严格的指令,并且返回 false 仍可能导致重新渲染组件。

这是文档中更深入的解释

于 2018-02-09T16:27:09.273 回答