赋值 width color 通过 ref nbsp his 单位
【React 设置宽度的坑】
我们知道通过 ref 可以获取 DOM 元素,通过 style 属性可以给此 DOM 元素添加样式。
但下面两行的赋值是无效的:
- this.HomeRootDiv.style.width=window.screen.width
- this.HomeRootDiv.style.height=window.screen.height
因为 style 中的 width、height 是有单位的,必须加上 px 才是有效的赋值。所以要改成下面这样
- this.HomeRootDiv.style.width=`${window.screen.width}px`
- this.HomeRootDiv.style.height=`${window.screen.height}px`
React 设置宽度的坑
来源: http://www.bubuko.com/infodetail-2278701.html