Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反应:将CSS类添加到body标记_Css_Reactjs - Fatal编程技术网

反应:将CSS类添加到body标记

反应:将CSS类添加到body标记,css,reactjs,Css,Reactjs,我想知道是否有更简单的方法将类添加到body标记 目前我正在做类似的事情 主要成分 const MAIN_CLASSES = [ three-col, darkTheme, blueHeader, .... etc] class MainComponent extends React.Component { componentDidMount() { document.body.classList.add(...MAIN_CLASSES); } component

我想知道是否有更简单的方法将类添加到body标记

目前我正在做类似的事情 主要成分

const MAIN_CLASSES = [ three-col, darkTheme, blueHeader, .... etc]

class MainComponent extends React.Component {
  componentDidMount() {
    document.body.classList.add(...MAIN_CLASSES);   
  }

  componentWillUnmount() {
   document.body.classList.remove(...MAIN_CLASSES);
  }

  render() ....
}

有兴趣知道别人在做什么谢谢

我能问一下为什么吗?如果您的React应用程序是在页面上唯一的元素
div中呈现的,那么为什么不将样式应用于根组件?感谢@glhrmv,我使用Khan Academy的aphrodite成功地将全局样式应用于body标签