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

Write a React website with a large button in the center that changes the background color

  1. User

    Write a React website with a large button in the center that changes the background color

  2. Assistant

    Here's an example React website that has a large button in the center that changes the background color:

    import React, { useState } from 'react';
    
    function App() {
      const [bgColor, setBgColor] = useState('#ffffff'); // default to white background
    
      const handleButtonClick = () => {
        // generate a random hex color code for the background
        const randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
        setBgColor(randomColor);
      }
    
      return (
        <div style={{ backgroundColor: bgColor, height: '100vh' }}>
          <button style={{ fontSize: '3em', padding: '1em', borderRadius: '0.5em' }} onClick={handleButtonClick}>
            Change Color
          </button>
        </div>
      );
    }
    
    export default App;
    

    This website has an App component that uses the useState hook to keep track of the background color of the div element. The button has an onClick handler that generates a random hex color code and sets it as the new background color. The div element has an inline style that sets the background color and height to fill the entire viewport. The button also has an inline style that makes it large and rounded, and the text inside of it says "Change Color".

  3. User

    Add a second button that changes a random image of a cat

  4. Assistant

    Sure! Here's an updated version of the App component that includes a second button that changes a random image of a cat:

    import React, { useState } from 'react';
    
    function App() {
      const [bgColor, setBgColor] = useState('#ffffff'); // default to white background
      const [catImage, setCatImage] = useState('https://cataas.com/cat'); // default cat image
    
      const handleColorButtonClick = () => {
        // generate a random hex color code for the background
        const randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
        setBgColor(randomColor);
      }
    
      const handleCatButtonClick = () => {
        // generate a random cat image url
        const catUrl = `https://cataas.com/cat/${Math.floor(Math.random() * 1000)}`;
        setCatImage(catUrl);
      }
    
      return (
        <div style={{ backgroundColor: bgColor, height: '100vh' }}>
          <button style={{ fontSize: '3em', padding: '1em', borderRadius: '0.5em' }} onClick={handleColorButtonClick}>
            Change Color
          </button>
          <br />
          <button style={{ fontSize: '3em', padding: '1em', borderRadius: '0.5em', marginTop: '1em' }} onClick={handleCatButtonClick}>
            Change Cat
          </button>
          <br />
          <img src={catImage} alt="random cat" style={{ maxWidth: '100%', maxHeight: '90vh', marginTop: '1em' }} />
        </div>
      );
    }
    
    export default App;
    

    The updated App component includes a new useState call that sets the state of the current cat image to a default value of https://cataas.com/cat. We've also added a new handleCatButtonClick function that generates a random integer between 0 and 999 to append to the end of the https://cataas.com/cat/ URL to generate a random cat image.

    The button JSX element has been duplicated and modified, with the second version of the button saying "Change Cat" and calling the handleCatButtonClick function when clicked.

    Finally, the img JSX element has been added below the buttons and will display the current cat image based on the catImage state.

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