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://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; function App() { const [arrayData, setArrayData] = useState([ { id: 1, name: "Element 1", enabled: true }, { id: 2, name: "Element 2", enabled: false }, { id: 3, name: "Element 3", enabled: true }, // Add more elements as needed ]); // Function to toggle the enabled/disabled status of an element const toggleElement = (id) => { setArrayData((prevState) => prevState.map((item) => { if (item.id === id) { return { ...item, enabled: !item.enabled }; } return item; }) ); }; return ( <div className="container"> <h3>How to enable/disable elements from an array in React Js?</h3> {arrayData.map((item) => ( <div key={item.id}> <input type="checkbox" checked={item.enabled} onChange={() => toggleElement(item.id)} /> <span>{item.name}</span> </div> ))} </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { max-width: 500px; margin: 0 auto; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .container div { display: flex; align-items: center; margin-bottom: 10px; } .container input[type="checkbox"] { margin-right: 10px; } .container span { font-size: 16px; } .container span:hover { color: #ff0000; /* Change the color to your preference */ cursor: pointer; } </style> </body> </html>