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> <!-- This is used to compile on runtime --> <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() { // Step 1: Create a state variable for numbers const [numbers, setNumbers] = useState({ num1: 34, num2: 45, num3: 67, }); const { num1, num2, num3 } = numbers; // Step 2: Create a state variable for the sum const [sum, setSum] = useState(0); // Step 3: Create a function to handle input changes const handleNumChange = (e, fieldName) => { const newValue = parseInt(e.target.value, 10); setNumbers({ ...numbers, [fieldName]: newValue }); }; // Step 4: Calculate the sum const calculateSum = () => { const result = num1 + num2 + num3; setSum(result); }; return ( <div className='container'> <h1 className='title'>React Js Add Three Numbers</h1> <input type="number" className='input' placeholder="Number 1" value={num1} onChange={(e) => handleNumChange(e, 'num1')} /> <input type="number" className='input' placeholder="Number 2" value={num2} onChange={(e) => handleNumChange(e, 'num2')} /> <input type="number" className='input' placeholder="Number 3" value={num3} onChange={(e) => handleNumChange(e, 'num3')} /> <button className='calculate-button' onClick={calculateSum}>Calculate Sum</button> <p className='sum-text'>Sum: {sum}</p> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 0 auto; width: 500px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; font-family: Arial, sans-serif; } .title { font-size: 24px; margin-bottom: 20px; color: #333; } .input { width: 200px; padding: 10px; margin: 5px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; outline: none; } .calculate-button { padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; } .calculate-button:hover { background-color: #0056b3; } .sum-text { font-size: 18px; margin-top: 20px; color: #333; } </style> </body> </html>