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() { // Create a ref to the image element const imageRef = useRef(null); // Function to change the image source const changeImage = () => { if (imageRef.current) { imageRef.current.src = "https://www.sarkarinaukriexams.com/images/import/sne20464172895.png"; } }; return ( <div className='container' > <h3>React Js use Refs to change image src</h3> <img ref={imageRef} src="https://www.sarkarinaukriexams.com/images/post/1684087085fantasy-gefbff9628_640.jpg" alt="Image" /> <button onClick={changeImage}>Change Image</button> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> /* Container */ .container { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); text-align: center; } /* Style for the image */ .container img { max-width: 100%; height: 200px; border-radius: 5px; margin-bottom: 15px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } /* Style for the button */ .container button { padding: 10px 20px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } /* Hover effect for the button */ .container button:hover { background-color: #0056b3; } </style> </body> </html>