Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Create a react website with a button that changes the background color

  1. User

    Create a react website with a button that changes the background color

  2. Assistant

    Here is a basic example of a React website with a button that changes the background color:

    import React, { useState } from "react";
    
    function App() {
      const [color, setColor] = useState("#ffffff"); // initial white color
    
      const handleClick = () => {
        const newColor = getRandomColor();
        setColor(newColor);
      };
    
      const getRandomColor = () => {
        const letters = "0123456789ABCDEF";
        let color = "#";
        for (let i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
      };
    
      return (
        <div style={{ backgroundColor: color, height: "100vh" }}>
          <button onClick={handleClick}>Change Color</button>
        </div>
      );
    }
    
    export default App;
    

    In this example, we're using React's useState hook to keep track of the current background color. We're also defining a handleClick function that generates a random color and sets it as the new background color using the setColor function. The getRandomColor function simply generates a random hex color code.

    We're then rendering a div with a style that sets the backgroundColor to the current color state, along with a button that triggers the handleClick function when clicked.

    Note that we're using inline styles in this example, but you could also define a CSS class and toggle it on the div element instead if you prefer.

  3. User

    Add a second button to generate a random cat image

  4. Assistant

    Sure, here's an updated version of the previous example with a second button that generates a random cat image from the Cat API:

    import React, { useState } from "react";
    
    function App() {
      const [color, setColor] = useState("#ffffff"); // initial white color
      const [imageUrl, setImageUrl] = useState(""); // empty initial image URL
    
      const handleColorClick = () => {
        const newColor = getRandomColor();
        setColor(newColor);
      };
    
      const handleImageClick = async () => {
        const response = await fetch("https://api.thecatapi.com/v1/images/search");
        const data = await response.json();
        const imageUrl = data[0].url;
        setImageUrl(imageUrl);
      };
    
      const getRandomColor = () => {
        const letters = "0123456789ABCDEF";
        let color = "#";
        for (let i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
      };
    
      return (
        <div style={{ backgroundColor: color, height: "100vh" }}>
          <button onClick={handleColorClick}>Change Color</button>
          <button onClick={handleImageClick}>Get Cat Image</button>
          <br />
          {imageUrl && <img src={imageUrl} alt="Random cat" />}
        </div>
      );
    }
    
    export default App;
    

    In this updated example, we're defining a new state variable imageUrl to keep track of the current cat image URL. We're also defining a new function handleImageClick that fetches a random cat image URL from the Cat API and sets it as the new imageUrl state using the setImageUrl function. We're then rendering an img element if imageUrl is not empty.

    We're also rendering a second button that triggers the handleImageClick function when clicked. Note that we're using the fetch function to make a GET request to the Cat API endpoint and the async/await keywords to handle the asynchronous response.

    Let me know if you have any questions!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.