Write a React website with a large button in the center that changes the background color
- User
Write a React website with a large button in the center that changes the background color
- 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
Appcomponent that uses theuseStatehook to keep track of the background color of thedivelement. The button has anonClickhandler that generates a random hex color code and sets it as the new background color. Thedivelement 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". - User
Add a second button that changes a random image of a cat
- Assistant
Sure! Here's an updated version of the
Appcomponent 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
Appcomponent includes a newuseStatecall that sets the state of the current cat image to a default value ofhttps://cataas.com/cat. We've also added a newhandleCatButtonClickfunction that generates a random integer between 0 and 999 to append to the end of thehttps://cataas.com/cat/URL to generate a random cat image.The
buttonJSX element has been duplicated and modified, with the second version of the button saying "Change Cat" and calling thehandleCatButtonClickfunction when clicked.Finally, the
imgJSX element has been added below the buttons and will display the current cat image based on thecatImagestate.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.