Trying to use React.DOM to set body styles

后端 未结 4 1365
情话喂你
情话喂你 2020-12-08 06:07

How can I use React.DOM to change styles on HTML body?

I tried this code and it\'s not working:

var MyView = React.createClass({
  rende         


        
4条回答
  •  囚心锁ツ
    2020-12-08 06:43

    Assuming your body tag isn't part of another React component, just change it as usual:

    document.body.style.backgroundColor = "green";
    //elsewhere..
    return (
      
    Stuff goes here.
    );

    It's recommended to put it at componentWillMount method, and cancel it at componentWillUnmount:

    componentWillMount: function(){
        document.body.style.backgroundColor = "green";
    }
    
    componentWillUnmount: function(){
        document.body.style.backgroundColor = null;
    }
    

提交回复
热议问题