将代码从componentWillMount()
移到componentDidMount()
:
componentDidMount() {
console.log('window.innerHeight', window.innerHeight);
}
下一个.js,componentDidMount()
仅在客户端上执行,其中window
和其他特定于浏览器的API将可用.从Next.js wiki人中:
下一个js是通用的,这意味着它首先在服务器端执行代码,
同样,componentWillMount()
在React的v17中将是deprecated,因此在不久的将来使用它实际上可能是不安全的.