Alternative
Amazon
Article
Writing
Art
AI
Angular
Photoshop
Premiere
Animal Crossing
Blog
Story
Android
Android Studio
Davinci Resolve
CSS
Clipchamp
ChatGPT
Crypto
DALL-E
Discord
Davinci Resolve
Davinci Resolve 18
Dream Studio
Express
Filmora
Flutter
PC Games
Git
GPT
GPT3
GPT4
GTA
GTA 6
Ghost Together
Ghost Together
HTML
iOS
iMovie
Illustrator
JavaScript
Mac
Mongo
Midjourney
Minecraft
Node.js
NBA 2k24
OSX
PHP
Palworld
Python
Playground AI
Roblox
React
Recipe (Cooking)
React Native
Semicolon.dev
Starfield PC Game
Snapchat
Steam
Stable Diffusion
SVG
Threads
Text To Image (AI)
VSCode
Vim
Web Development
Windows
WebGL
React Tutorials

    React: Change BG Color Dynamically On Button Click

    Changing background color of entire page or component on mouse click

    #react #reactjs #change #background #color #click #dynamic
    viewed 1.7K times
    › react › Change BG Color In React

    So you tried to do a simple thing in React. But it isn't so easy. So in this tutorial I'll show you full source code for how to change bg color of the body element in React.

    If you don't like reading, you can watch this how to change background color on mouse click tutorial on Semicolon's YouTube channel.

    How to change background color of component in React dynamically on mouse click.

    How to change background color of entire page in HTML

    React doesn't do things the same way as vanilla JavaScript, or other frameworks.

    Even simple things like changing background color on mouse click might not be trivial, and require some basic background on how React apps work. In this tutorial I'll show you the minimum code to change background color of entire page dynamically on button click.

    To change background color on mouse click in React, follow these steps:

    1. Import useState and useEffect hooks from React library
    2. Create color variable and setter function [color, setColor] with useState hook
    3. Create a button inside constructor of the main app component
    4. Write click function that changes color using setColor hook method
    5. Add the click function to the button's onClick event
    6. Create new useEffect hook with [color] dependency to assign the state hook's color to JavaScript's document.body.style.backgroundColor property.

    The bottom line is you want to set color to document.body.style.backgroundColor property in your useEffect hook, not anywhere else in the app.

    Putting it together, here is the entire source code:

    import { useState, useEffect } from 'react'
    
    function App() {
    
      const [color, setColor] = useState("blue")
    
      const click = color => {
        setColor(color)
      }
    
      /* This is where we actually
         change background color */
      useEffect(() => {
        document.body.style.backgroundColor = color
      }, [color])
    
      /* Display clickable
          button */
      return (<div className = "App">
        <button onClick = {
          () => click("yellow")
        }>Change BG Color</button>
      </div>)
    }
    
    export default App;

    I know this might look complicated, but we need both useState, and useEffect hooks in order to do something as simple as change background color in React.

    Please share this article if it helped you make background color change dynamically on click of a button. This way we can help more people learn how to do this the proper way in React.

    Let's Ghost Together
    Sign Up Now  -  It's Free!
    Write For Us
    Sign Up Now  -  It's Free!

    React: Change BG Color Dynamically On Button Click

    Comments (2) New! (You can now post comments on articles!)

    (By posting a comment you are registering a Ghost Together account.)
    Register
    Register
    Post It
    DM Coming Soon
    f f