Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

button color change loop in react

import React, { useState } from "react";

export default function App() {
  const Colors = ["red", "green"];
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {Colors.map((color, key) => (
        <Button Color={color} Colors={Colors} key={key} />
      ))}
    </div>
  );
}

function Button({ Color, Colors }) {
  const [color, setColor] = useState(Color);
  const handleChange = (e) => {
    e.preventDefault();
    setColor(Colors[Math.floor(Math.random() * Colors.length)]);
  };
  const style = {
    backgroundColor: color
  };
  return (
    <button onClick={handleChange} style={style}>
      Click Me!
    </button>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: link the filename to the visible layer 
Javascript :: mongoose populate not working 
Javascript :: misturar dois arrays javascript 
Javascript :: js version error 
Javascript :: open json file in JS in order to access data 
Javascript :: Using anonymous functions as arguments of other functions 
Javascript :: can i use promise.all conditional in vue js 
Javascript :: js multiple declaration 
Javascript :: nested while loop in javascript 
Javascript :: add button to add item javascript 
Javascript :: Confirm the EndingPassed--Javascript 
Javascript :: Detect backspace pressed eventlistener 
Javascript :: jsmodule not installed vuejs webstorm 
Javascript :: Rectangle star pattern in JavaScript 
Javascript :: i wanted to detect when a user enters an alphabet key in input text javascript 
Javascript :: cant resolve module after typescript install 
Javascript :: Dynamically Generated Table 
Javascript :: online python to c++ converter 
Javascript :: nodejs process object 
Javascript :: Replacing Specific word from url, replacing url 
Javascript :: matrix addition in javascript 
Javascript :: mreact graph 
Javascript :: how to input struct into parameter in remix 
Javascript :: remember previous window javascript 
Javascript :: make a backend server in node 
Javascript :: disable jquery ajax call on init 
Javascript :: key index split 
Javascript :: Foreach array in JavaScript fsd 
Javascript :: complicated reduce 
Javascript :: why typescript is superset of javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =