React js open image new tab onclick image
React js Open Image New Tab onclick Image:To open an image in a new tab in a React.js application upon clicking it, you can create a click event handler for the image. When the image is clicked, this handler can use the window.open()
method to open the image URL in a new browser tab.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I open an image in a new tab when a user clicks on it in a Reactjs?
This React js code defines a simple component, "App," that displays an image and allows users to open it in a new browser tab when clicking on it. The image URL is provided, and a function, openImageInNewTab
, is triggered when the image is clicked. This function uses window.open
to open the image URL in a new tab. The image also has a pointer cursor to indicate it's clickable. When rendered, users can click the image, and it will open in a new tab.
React js Open Image New Tab onclick Image Example
<script type="text/babel">
const { useState, useEffect, useRef } = React;
function App() {
const imageUrl = 'https://www.sarkarinaukriexams.com/images/import/sne4446407201.png';
const openImageInNewTab = () => {
window.open(imageUrl, '_blank');
};
return (
<div className='container'>
<h3>React js Open Image New Tab onclick Image</h3>
<p>Click the image to open it in a new tab:</p>
<img
src={imageUrl}
alt="Click to open in new tab"
onClick={openImageInNewTab}
style={{ cursor: 'pointer' }}
/>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>