React native React navigation获取类组件内的堆栈导航器标题高度(无挂钩)

React native React navigation获取类组件内的堆栈导航器标题高度(无挂钩),react-native,react-navigation,react-navigation-v5,React Native,React Navigation,React Navigation V5,我知道可以使用以下挂钩(V5)获得堆栈标题高度: 类组件是否有解决方法?目前,我正在使用上下文使用者获取render()函数内部的高度: <HeaderHeightContext.Consumer> {headerHeight => ( ... )} </HeaderHeightContext.Consumer> {人头高度=>( ... )} 但是我希望在render()函数之外使用headerHei

我知道可以使用以下挂钩(V5)获得堆栈标题高度:

类组件是否有解决方法?目前,我正在使用上下文使用者获取render()函数内部的高度:

<HeaderHeightContext.Consumer>
                  {headerHeight => (   
    ...
    )}
</HeaderHeightContext.Consumer>

{人头高度=>(
...
)}

但是我希望在render()函数之外使用headerHeight。

您是否在组件中尝试过类似以下内容:

static contextType = HeaderHeightContext;

componentDidMount() {
   console.log(this.context); // this is your height
}
发件人:

可以为类的contextType属性分配一个由React.createContext()创建的上下文对象。这允许您使用This.Context使用该上下文类型最近的当前值


我得到一个:
TypeError:undefined不是一个对象(计算''u this.context.headerHeight')
我是这样测试的:
class AddProductScreen extensed React.Component{static contextType=HeaderHeightContext;构造函数(props){super(props);const{headerHeight}=this.context;console.log(headerHeight)?
static contextType = HeaderHeightContext;

componentDidMount() {
   console.log(this.context); // this is your height
}