screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.14.6/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel" data-presets="env,react"> const { useState } = React; function App() { // Step 1: Create a state variable to store the current color value const [isClick, setIsClick] = useState(true); // Set the initial color to red // Step 3: Implement a function to toggle the color when the button is clicked const toggleColor = () => { setIsClick(!isClick); // Toggle the state between true and false }; return ( <div className="container"> <h3>React Js Change Button Color onclick using inline style</h3> {/* Step 2: Use a ternary operator in the button's inline style to conditionally set the color */} <button style={{ backgroundColor: isClick ? "#00897B" : "#26C6DA", }} onClick={toggleColor} > Change Button Color onclick </button> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { max-width: 500px; margin: 0 auto; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); font-family: Arial, sans-serif; } button { font-size: 18px; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease-in-out, color 0.3s ease-in-out; color:#fff } </style> </body> </html>