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, useEffect } = React; function App() { const [imageKey, setImageKey] = useState(0); const [imageLoaded, setImageLoaded] = useState(false); useEffect(() => { setImageLoaded(false); // Reset the imageLoaded state when imageKey changes }, [imageKey]); const handleClick = () => { setImageKey(prevKey => prevKey + 1); }; const handleImageLoad = () => { setTimeout(() => { setImageLoaded(true); }, 100); // Add a slight delay to allow the transition effect to take effect }; return ( <div className="container"> <h3>React Js reload/refresh image with same URL onClick</h3> <img src={`https://www.sarkarinaukriexams.com/images/editor/1670265927Capture123243243423.PNG?key=${imageKey}`} alt="Reloadable Image" className={imageLoaded ? 'fade-in' : 'fade-out'} onLoad={handleImageLoad} onClick={handleClick} /> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { 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); } .fade-in { opacity: 1; transition: opacity 0.5s; } .fade-out { opacity: 0; transition: opacity 0.5s; } img { max-width: 100%; height: 250px; cursor: pointer; } </style> </body> </html>