How to generate random colors by using React hooks ? Last Updated : 09 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In web development, colors play a vital role in creating visually appealing and engaging user interfaces. In React we may need to generate random colors dynamically. In this article, we will explore how to achieve this using React hooks, a powerful feature introduced in ReactJs. Pre-requisite:NPM & Node.jsReact.jsReact HooksApproach:To generate random colors by using the React hook we will be creating a random color generator custom hook, here we will make a function to generate color according to user input. Adding an event handler with a button to change the color of our window. Steps to Create React Application:Step 1: Go to your command prompt and write the below command to create a react app. npx create-react-app <YOUR_APP_NAME>Step 2: Then go to your app folder by typing the below command cd <YOUR_APP_NAME>Project Structure: Folder structureExample: This example implements a custom hook to generate random color and render it as the background color. JavaScript // Filename - App.js import "./App.css"; import useGenerateRandomColor from "./useGenerateRandomColor"; function App() { const { color, generateColor } = useGenerateRandomColor(); return ( <div style={{ height: "100vh", width: "100vw", backgroundColor: "#" + color, display: "flex", justifyContent: "center", alignItems: "center", }} > <button style={{ padding: "40px", borderRadius: "10px", backgroundImage: "linear-gradient(to top, #a8edea 0%, #fed6e3 100%)", fontSize: "larger", }} onClick={generateColor} > Generate random color </button> </div> ); } export default App; JavaScript // Filename - useGenerateRandomColor.js import { useState } from 'react'; const useGenerateRandomColor = () => { const [color, setColor] = useState("") const generateColor = () => { setColor(Math.random().toString(16).substr(-6)); }; return { color, generateColor }; }; export default useGenerateRandomColor; Step to run the application: Run the following command to start your app in your localhost:3000. npm startOutput: This output will be visible on the http:// localhost:3000 on the browser window. Comment More infoAdvertise with us Next Article How to generate random colors by using React hooks ? M megha25 Follow Improve Article Tags : Misc Web Technologies ReactJS React-Questions React-Hooks +1 More Practice Tags : Misc Similar Reads How to create a Color-Box App using ReactJS? Basically we want to build an app that shows the number of boxes which has different colors assigned to each of them. Each time the app loads different random colors are assigned. when a user clicks any of the boxes, it changes its color to some different random color that does not equal to its prev 4 min read Build a Random Name Generator using ReactJS In this article, a Random Name GeÂnerator will be created using React.js. Building a Random Name Generator means creating a program or application that generates random names, typically for various purposes like usernames, fictional characters, or data testing. It usually involves combining or selec 4 min read Random Quote Generator App using ReactJS In this article, we will create an application that uses an API to generate random quotes. The user will be given a button which on click will fetch a random quote from the API and display it on the screen. Users can generate many advices by clicking the button again. The button and the quotes are d 3 min read Color Palette Generator app using React Color Palette Generator App using ReactJS is a web application which enables useÂrs to effortlessly geneÂrate random color palettes, vieÂw the colors, and copy the color codes to the clipboard with just a single click. There is also a search bar which allows the user to check different color themes 5 min read Create a Random Joke using React app through API In this tutorial, we'll make a website that fetches data (joke) from an external API and displays it on the screen. We'll be using React completely to base this website. Each time we reload the page and click the button, a new joke fetched and rendered on the screen by React. As we are using React f 3 min read Like