Create a react website with a button that changes the background color
- User
Create a react website with a button that changes the background color
- 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
useStatehook to keep track of the current background color. We're also defining ahandleClickfunction that generates a random color and sets it as the new background color using thesetColorfunction. ThegetRandomColorfunction simply generates a random hex color code.We're then rendering a
divwith a style that sets thebackgroundColorto the current color state, along with abuttonthat triggers thehandleClickfunction 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
divelement instead if you prefer. - User
Add a second button to generate a random cat image
- 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
imageUrlto keep track of the current cat image URL. We're also defining a new functionhandleImageClickthat fetches a random cat image URL from the Cat API and sets it as the newimageUrlstate using thesetImageUrlfunction. We're then rendering animgelement ifimageUrlis not empty.We're also rendering a second
buttonthat triggers thehandleImageClickfunction when clicked. Note that we're using thefetchfunction to make a GET request to the Cat API endpoint and theasync/awaitkeywords to handle the asynchronous response.Let me know if you have any questions!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.