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://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 { useState } = React; const App = () => { const [userName, setUserName] = useState(''); const handleUserNameChange = (event) => { setUserName(event.target.value); }; const handleLogin = () => { if (userName.trim() !== '') { // Store the user name in session storage sessionStorage.setItem('userName', userName); alert('User name stored in session storage'); } else { alert('Please enter a valid user name'); } }; const retrieveUserName = () => { const storedUserName = sessionStorage.getItem('userName'); if (storedUserName) { return <h2>Welcome, {storedUserName}!</h2>; } else { return <h2>Please log in first.</h2>; } }; return ( <div className='container'> <h1>React Js Session Storage Example</h1> <input type="text" placeholder="Enter your user name" value={userName} onChange={handleUserNameChange} /> <button onClick={handleLogin}>Login</button> {retrieveUserName()} </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { text-align: center; padding: 20px; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); margin: 0 auto; max-width: 600px; } input { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 5px; box-sizing: border-box; } button { padding: 10px 20px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } button:hover { background-color: #0056b3; } /* Additional styling for the retrieved username */ #retrievedUsername { font-size: 18px; margin-top: 20px; color: #555; } </style> </body> </html>