【问题标题】:randomly change the background color of a react app by a click通过单击随机更改 React 应用程序的背景颜色
【发布时间】:2018-12-21 01:31:29
【问题描述】:

我尝试通过单击一个按钮来随机更改整个页面的背景颜色,但它只是更改了 div 元素的背景。这是我的代码

  import React from "react";
  class Home extends React.Component {
      constructor(props) {
    super(props);
    this.state = {
        quotes: ["red", "yellow", "blue", "green", "purple", "pink"],
        colors: [],
    };
    this.nextColor = this.nextColor.bind(this);
}
// moves to display next quotes 
nextColor() {
    const random = this.state.quotes[Math.floor(Math.random() * this.state.quotes.length)]
    console.log("receiving ", random);
    this.setState({
        colors: random
    })
}
render() {
    return (
        <div style={{backgroundColor: this.state.colors}}>
            <h1>{this.state.colors}</h1>
            <button onClick={this.nextColor}>Next Color</button>
        </div>
    )
  }
  }

   ReactDOM.render(<Home />, document.getElementById("app")); 

单击 nextColor 按钮时,我需要更改整个页面的背景颜色。您的帮助将不胜感激

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    body 标签不受 React 控制。修改 body 标记挂钩到 componentDidMount 生命周期并使用 vanilla javascript 设置颜色

    这里有办法https://jaketrent.com/post/update-body-class-react/

    简而言之,添加以下逻辑

      componentDidMount() {
        document.body.style.backgroundColor = this.state.colors;
      }
    

    【讨论】:

    • 它仍然会改变 div 背景而不是整个页面
    【解决方案2】:

    Ramesh 给出正确答案,这里是演示:

    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          colors: ["red", "yellow", "blue", "green", "purple", "pink"]
        };
      }
    
      changeBg() {
        const { colors } = this.state;
        const color = colors[Math.floor(Math.random() * colors.length)];
        document.body.style.backgroundColor = color;
      }
    
      render() {
        return (
          <div>
            <button onClick={() => this.changeBg()}>Change Color</button>
          </div>
        );
      }
    }
    
    ReactDOM.render(
      <App />,
      document.getElementById('root')
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <div id="root"></div>

    【讨论】:

      【解决方案3】:
      import React from "react";
      
      function MyComponent({ name }) {
        // I found this formula
        // here: https://css-tricks.com/snippets/javascript/random-hex-color/
        let randomColor = Math.floor(Math.random() * 16777215).toString(16);
      
        // The trouble I had was about how to use
        // the variable randomColor in "style:{}" tag
        return (
          <div className="parent-container">
            <div
              className="child-container"
              style={{ backgroundColor: "#" + `${randomColor}` }}
            >
              <h4 className="name">{name}</h4>
            </div>
          </div>
        );
      }
      
      export default MyComponent;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-07
        • 2022-11-22
        • 2018-03-24
        • 2018-09-19
        • 1970-01-01
        • 2022-07-11
        • 2017-01-15
        • 2018-11-10
        相关资源
        最近更新 更多