簡體   English   中英

反應中 getDerivedStateFromProps 中的訪問上下文

[英]access context in getDerivedStateFromProps in react

我正在使用類來使用 React.createContext

const config = React.createContext()

class MyApp extends Component {

  static contextType = TenantConfig

  static getDerivedStateFromProps(){
  this.context // I can't do this here
}

  render(){
    return <div>anything</div>
  }
}

MyApp.contextType = config
export default MyApp

如何在 getDerivedStateFromProps 中訪問 this.context?

不能在靜態函數中使用this關鍵字。

您始終可以使用componentDidMountcomponentDidUpdate進行任何比較。

暫無
暫無

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

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