screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useEffect, useState } = React; const App = () => { const [isBodyClassActive, setIsBodyClassActive] = useState(false); useEffect(() => { if (isBodyClassActive) { document.body.classList.add("active"); } else { document.body.classList.remove("active"); } }, [isBodyClassActive]); const toggleBodyClass = () => { setIsBodyClassActive(!isBodyClassActive); }; return ( <div className="container"> <h3>React Js Add Remove class to Body Tag | Toggle class</h3> <button onClick={toggleBodyClass}>Toggle Body Class</button> </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> body.active { background-color: lightblue; } .container { text-align: center; margin-top: 100px; } button { padding: 10px 20px; font-size: 18px; background-color: #4caf50; color: white; border: none; cursor: pointer; } </style> </body> </html>