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 { useRef } = React; function App() { const elementRefs = [ useRef(null), useRef(null), useRef(null) ]; // Your logic to remove the element using the ref const removeElement = (index) => { const element = elementRefs[index].current; if (element) { element.remove(); } }; return ( <div className='container'> <h3>React Js Remove Elements by Ref</h3> <div className='element' ref={elementRefs[0]}> Element 1 to be removed <button className='remove-button' onClick={() => removeElement(0)}> Remove Element </button> </div> <div className='element' ref={elementRefs[1]}> Element 2 to be removed <button className='remove-button' onClick={() => removeElement(1)}> Remove Element </button> </div> <div className='element' ref={elementRefs[2]}> Element 3 to be removed <button className='remove-button' onClick={() => removeElement(2)}> Remove Element </button> </div> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { width: 400px; margin: 0 auto; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); } .element { padding: 10px; margin: 10px; background-color: #ffffff; border: 1px solid #dddddd; border-radius: 4px; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1); text-align: center; } .remove-button { padding: 8px 16px; background-color: #ff6347; color: #ffffff; border: none; border-radius: 4px; cursor: pointer; margin-left: 5px; transition: background-color 0.3s; } .remove-button:hover { background-color: #e74c3c; } </style> </body> </html>