The <PostLink> <Link> component in NextJs with Typescript is not displaying the expected PostLink as demonstrated in the tutorial

I encountered an issue where the / tags are not working as expected. Contrary to the tutorial, nothing was displayed after compiling.

Following the guide on Nextjs.org, I utilized the typescript version with create-next-app typescript. Despite overcoming various type-related obstacles, I managed to compile successfully but ended up with a blank component. Upon checking the console, I received the warning message: index.js:1 Warning: the 'url' property is deprecated.

After clicking the provided link and following the instructions, including downloading the codemod, I still faced the same issue. Any suggestions on what steps I should take next? Thank you in advance!

import * as React from "react";
import Layout from '../components/MyLayout'
import Link from "next/link";
import {withRouter} from 'next/router'

interface IPostLinkProps{
  title:string
}

const PostLink = (props:IPostLinkProps) =>{
 return( 
    <li>
    <Link href={`/post?title=${props.title}`}>
      <a>{props.title}</a>
    </Link>
  </li>
 )
}

export default withRouter(function Blog(){
    return(
      <Layout>
        <h1> My Blog</h1>
          <ul>
              <PostLink title='hi'/>
              <PostLink title="HELP!"/>
              <PostLink title='hi'/>
          </ul>
      </Layout>
  )
})

index.js:1 Warning: the 'url' property is deprecated.

Answer №1

When in need of assistance, consider incorporating the useRouter function. Import { useRouter } from 'next/router';

Answer №2

Give this a try:

interface IProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
    title: string
}

export default function CustomLink({content, uri, title, ...other}: IProps) {
    return <NextLink passHref {...{uri}}>
        <a {...other}>{title ?? content}</a>
    </NextLink>
}

Use it by:

<CustomLink uri='/' title='Homepage'/>

or

<CustomLink uri='/'>
  <span>Homepage</span>
</CustomLink>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on transferring an image from Electron to Next.js using drag and drop feature

I am facing difficulties implementing drag and drop functionality in Electron when integrating with Next.js. I have been following an official Electron tutorial on drag and drop and referring to an example from the official Next.js repository. After follow ...

Tips for deleting multiple objects from an array in angular version 13

I am facing an issue where I am trying to delete multiple objects from an array by selecting the checkbox on a table row. However, I am only able to delete one item at a time. How can I resolve this problem and successfully delete multiple selected objects ...

The action 'addSection' is restricted to private access and can only be utilized internally by the class named 'File'

const versionList = Object.keys(releaseNote) for (const key of versionList) { // Skip a version if none of its release notes are chosen for cherry-picking. const shouldInclude = cherryPick[key].some(Boolean) if (!shouldInclude) { continue } // Include vers ...

Next.js is throwing a TypeError because it is unable to convert undefined or null to an object

Whenever I try to run my project locally, I encounter the 'TypeError: Cannot convert undefined or null to object' error as shown in the screenshot below. I've attempted updating all the packages listed in package.json. Furthermore, I delete ...

Troubleshooting issue with getServerSideProps not functioning in Next.js while utilizing Next-redux-wrapper and TypeScript

When attempting to trigger an action as outlined in the documentation using the getServerSideProps function with the help of next-redux-wrapper store and redux-thunk, I am encountering the following TypeScript error: ts(2322): Type '({ req }: GetServe ...

Deriving data types based on a variable in TypeScript

If I have a dictionary that links component names to their corresponding components like this: const FC1 = ({prop}: {prop: number}) => <>{prop}</>; const FC2 = ({prop}: {prop: string}) => <>{prop}</>; const mapComponents = [ ...

Oops! The OPENAI_API_KEY environment variable seems to be missing or empty. I'm scratching my head trying to figure out why it's not being recognized

Currently working on a project in next.js through replit and attempting to integrate OpenAI, but struggling with getting it to recognize my API key. The key is correctly added as a secret (similar to .env.local for those unfamiliar with replit), yet I keep ...

How to access the audio element in Angular using ViewChild: Can it be treated as an

My goal is to include an audio element inside a component. Initially, I approached this by using traditional methods: $player: HTMLAudioElement; ... ngOnInit() { this.$player = document.getElementById('stream') } However, I wanted to follow T ...

What is the best approach to implement server-side rendering in Next.js while utilizing Apollo React hooks for fetching data from the backend?

I have a Next.js project that is utilizing Apollo GraphQL to retrieve data from the backend. My goal is to render the page using server-side rendering. However, I am encountering an obstacle as the React hooks provided by GraphQL Apollo prevent me from cal ...

I keep encountering a network error whenever I try to refresh the page. What could be causing this issue? (

Whenever I make a GET request to an API using useEffect(), everything works fine when I navigate from the homepage to the page. However, if I refresh the page at http://localhost:3000/coins/coin, I encounter an error message saying "Unhandled Runtime Error ...

Error: Unable to access 'price' property of undefined - Next.js version 14.0.1

I'm encountering an issue with Next.js where my code is not working as expected. Interestingly, the same code works perfectly fine in other templates. let subTotal = 0 if (selectedProducts?.length) { for (let id of selectedProducts) { ...

Having trouble with React Hook Form controlled input and typing

My application utilizes the react-hook-forms library along with the office-ui-fabric-react framework. To integrate the framework inputs, I wrap the 3rd party component using the <Controller> element. The current setup is functional as shown below: ...

Is there a way to inform TypeScript that the process is defined rather than undefined?

When I execute the code line below: internalWhiteList = process.env.INTERNAL_IP_WHITELIST.split( ',' ) An error pops up indicating, Object is possibly undefined. The env variables are injected into process.env through the utilization of the mod ...

Alert: Potential shared module issue detected when updating swr from version 1 to version 2 within a NextJS application

I have decided to upgrade my swr package from version 1 to the latest version 2. Here is a glimpse of my package.json. I am currently utilizing React 18, NextJS 12, and Webpack 5 for this project, including the ModuleFederationPlugin integration. { " ...

How can I enable dragging functionality for components (not elements) in angular?

While utilizing the Angular CDK drag and drop module, I have observed that it functions seamlessly on html elements like div, p, etc. However, when I apply a cdkDrag directive to a component, it does not seem to work as expected. <!-- IT WORKS --> ...

Limitation for class instance, not an object

Is it possible to implement type constraints for class instances only in TypeScript, without allowing objects? Here is an example of what I am trying to achieve: class EmptyClass {} class ClassWithConstructorParams { constructor (public name: string) ...

Hold off on proceeding until the subscription loop has finished

Is there a way to verify that all results have been successfully pushed to nodesToExpand after running the for loop? The getFilterResult function is being called via an HTTP request in the nodeService service. for(var step in paths) { this.nodeSe ...

In order for Angular jQuery click events to properly function, they must be triggered by a

My admin panel is built using jQuery and Bootstrap, and it functions properly when used outside of the Angular framework. However, after integrating jQuery and Bootstrap into an Angular project and configuring them, I noticed that I had to double click on ...

Guide on transitioning an Angular 4 project created in Visual Studio 2015 to Angular 6 with Visual Studio Code

Currently, I am collaborating on an Angular 4 project that utilizes a web API in Visual Studio 2015 update 3. This project serves as an ERP solution. My goal is to enhance the project by updating it to Angular 6, with Visual Studio Code as the primary too ...

Automatically adjust the model input (Signal) based on the parent and respond to any alterations in the children

Within my Angular 16 application, I have a parent component that passes a plain JavaScript object (myObj) to a child component, where it is treated as a model<MyObj>. <!-- parent.component.html --> <app-children [myObjModel]="myObj&qu ...