screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <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 { useState } = React; function App() { const initialState = { name: '', country: '', salary: null, email: '', department: '', position: '', }; const [person, setPerson] = useState(initialState); const updateState = () => { setPerson({ name: 'Eleanor', country: 'Canada', salary: 150, email: 'eleanor@example.com', department: 'Engineering', position: 'Software Developer', }); }; const resetState = () => { setPerson(initialState); }; return ( <div className='container'> <h3>React Js Reset to initial State using hooks</h3> <button className='action-button' onClick={updateState}>Set state</button> <button className='action-button' onClick={resetState}>Reset to initial state</button> <pre className='employee-info'> {JSON.stringify(person, null, 4)} </pre> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background-color: #f0f0f0; font-family: Arial, sans-serif; } .action-button { padding: 10px 20px; margin: 10px; font-size: 16px; border: none; background-color: #3498db; color: #fff; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } .action-button:hover { background-color: #2980b9; } .employee-info { margin-top: 20px; padding: 20px; border-radius: 5px; background-color: #fff; border: 1px solid #ddd; max-width: 80%; overflow-x: auto; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); white-space: pre-wrap; } </style> </body> </html>