How to add a link to an Image in Nextjs

Onitololaurence
2 min readJan 28, 2023

--

What’s Nextjs?

Next.js is a JavaScript framework for building web applications. It is built on top of React, a popular JavaScript library for building user interfaces, and provides a set of features for building server-rendered React applications. Some of the key features of Next.js include automatic code splitting, server-side rendering, and a development server with hot module reloading. It also offers a simple and powerful way to manage the routing and the data fetching for your application.

Add image to Nextjs

To add an image to a Next.js application, you can use the standard img HTML tag and point the src attribute to the location of the image file. If the image is located within the project's public directory, you can simply reference it using the file's path. For example:

<img src=”/images/example.jpg” alt=”Example Image” />

Alternatively, you can also import the image and use it within your component like this:

import exampleImage from ‘./example.jpg’

const MyComponent = () => (
<img src={exampleImage} alt=”Example Image” />
)

In this case, the file should be located inside the component folder. Also, the file should be in web-supported format such as jpeg, png, gif etc. Please note that, you should use the correct image file format, and it should be compatible with web and browser.

How to solve this issue of page could not be found:

Solution:

You can add a link to an image in Next.js by wrapping the image in an anchor (<a>) tag and providing the desired link destination as the href attribute. For example:

<a href=”https://www.example.com">
<img src=”image.jpg” alt=”example image”>
</a>

This would create an image that, when clicked, would take the user to the URL “https://www.example.com". You can also use dynamic URLs with the help of JSX, by adding the link to state or props.

const ImageLink = ({src, href}) => {
return (
<a href={href}>
<img src={src} alt=”example image” />
</a>
)
}

You can use it in your component like this:

<ImageLink src=”image.jpg” href=”https://www.example.com"/>

Thanks for reading.

--

--

Onitololaurence
Onitololaurence

No responses yet