添加CSS以响应创建的元素,如data-reactroot

use*_*421 20 reactjs

根据我的根div反应"自动"创建另一个div.有没有办法为该div添加一个类?我需要添加高度:100%以防止背景内容在显示叠加层时在移动设备中滚动.

这是我检查网站上的元素时的显示方式.单击按钮时,我需要将高度:100%添加到data-reactroot div.但是我的源代码中没有这个div.

<div id="root">
    <div data-reactroot data-reactid="1" data-react-checksum="161698...
Run Code Online (Sandbox Code Playgroud)

我可以在容器的componentDidMount()中添加此代码

    let root = document.querySelectorAll('[data-reactroot]')[0];
    if (root) {
        root.style.height = '100%';
    }
Run Code Online (Sandbox Code Playgroud)

但是不是有更好的方法吗?这感觉就像一种hacky(以一种糟糕的方式)

Fab*_*ltz 21

#root > [data-reactroot] { height: 100% } 在你的CSS应该工作.

或者只是标签本身:

[data-reactroot] { ... }
Run Code Online (Sandbox Code Playgroud)