簡體   English   中英

如何在反應中卸載和重新安裝組件?

[英]How can I unmount and remount a component in react?

我有一個組件,並用錯誤邊界包圍它。

<ErrorBoundary>
   <ChildComponent />
</ErrorBoundary>

它在<ErrorBoundary />看起來像這樣:

export class ErrorBoundary extends React.Component {
    constructor() {
        super();
    }

    static getDerivedStateFromError(error) {
    }

    componentDidCatch(err, info) {
        console.log('error is caught in the errorbound')
    }

    componentDidUpdate(prevProps) {
        if (prevProps.errorSwitch === this.props.errorSwitch) return;
        console.log('there was an error in the parent component')
        this.forceUpdate()
    }

    render() {
        return this.props.children
    }
}

我如何強制this.props.children (子組件)1)卸載,然后2)再次重新安裝?

我不是在尋找如何使用新道具重新渲染,因為這個答案顯示了如何做。 我特意想卸載並重裝。

return { componentVisible && this.props.children}其中componentVisible是一個用作切換的prop,理想情況下是一個boolean。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM