<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>
<script type="text/babel">
const { useRef } = React;
// Create a ref to the image element
const imageRef = useRef(null);
// Function to change the image source
const changeImage = () => {
imageRef.current.src = "https://www.sarkarinaukriexams.com/images/import/sne20464172895.png";
<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>
ReactDOM.render(<App />, document.getElementById('app'));
font-family: Arial, sans-serif;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
/* Style for the image */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
/* Style for the button */
background-color: #007bff;
transition: background-color 0.3s ease;
/* Hover effect for the button */
.container button:hover {
background-color: #0056b3;