加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React合成的`window`对象?

发布时间:2020-12-15 20:15:53 所属栏目:百科 来源:网络整理
导读:我想知道传入反应组件的窗口对象是否合成. 如果不是我假设我需要以下代码来确定视口的宽度和高度 const w = window,d = document,documentElement = d.documentElement,body = d.getElementsByTagName('body')[0],width = w.innerWidth || documentElement.c
我想知道传入反应组件的窗口对象是否合成.

如果不是我假设我需要以下代码来确定视口的宽度和高度

const w = window,d = document,documentElement = d.documentElement,body = d.getElementsByTagName('body')[0],width = w.innerWidth || documentElement.clientWidth || body.clientWidth,height = w.innerHeight || documentElement.clientHeight || body.clientHeight

解决方法

window对象是普通的DOM对象.

问题是它仅在组件安装后才可用.

componentDidMount () {
  const w = window,height = w.innerHeight || documentElement.clientHeight || body.clientHeight
}

https://facebook.github.io/react/docs/react-component.html#componentdidmount

更新:

如果DOM在React之前加载,则窗口对象在UNSAFE_componentWillMount中可用.只要您没有在文档的< head>中加载React,我认为使用UNSAFE_componentWillMount就可以了.

因为this而加入UNSAFE
,您应该在将来的版本中使用componentDidMount …

注意:即将渲染的实际组件将不存在,但窗口可能存在.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读