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 [items, setItems] = useState([ { id: 1, name: 'United States', population: 331449281 }, { id: 2, name: 'China', population: 1444216107 }, { id: 3, name: 'India', population: 1393409038 }, { id: 4, name: 'Indonesia', population: 276361783 }, { id: 5, name: 'Pakistan', population: 225199937 }, { id: 6, name: 'Brazil', population: 213993437 }, { id: 7, name: 'Nigeria', population: 211400708 }, { id: 8, name: 'Bangladesh', population: 170694575 }, { id: 9, name: 'Russia', population: 146599183 }, { id: 10, name: 'Mexico', population: 129166028 } ]); const removeItem = (id) => { const index = items.findIndex(item => item.id === id); if (index !== -1) { const updatedItems = [...items]; updatedItems.splice(index, 1); setItems(updatedItems); } }; return ( <div className="container"> <h3>React Js Remove/Delete item from array by id</h3> <ul> {items.map(item => ( <li key={item.id}> {item.name} {item.population} <button onClick={() => removeItem(item.id)}>Remove</button> </li> ))} </ul> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; 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; } .container h3 { font-size: 24px; margin-bottom: 10px; } .container ul { list-style: none; padding: 0; } .container li { margin-bottom: 10px; display: flex; align-items: center; } .container li button { margin-left: 10px; padding: 5px 10px; background-color: #dc3545; color: white; border: none; border-radius: 5px; cursor: pointer; } .container li button:hover { background-color: #c82333; } </style> </body> </html>