When utilizing Next.js with TypeScript, you may encounter an error message stating that the property 'className' is not found on the type 'IntrinsicAttributes & IntrinsicClassAttributes'

I recently put together a basic nextjs app using typescript. Here's the link to the example: https://github.com/zeit/next.js/tree/master/examples/with-typescript

However, I encountered an issue where I am unable to add the className attribute to any element. When trying to do so, I receive the following error message: Property 'className' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly & Readonly<{ children?: ReactNode; }>

In addition, I'm also facing a type error for other attributes like 'rel' on the link element.

https://i.stack.imgur.com/QDNdH.png

https://i.stack.imgur.com/pUPml.png

Answer №1

Refer to the official NextJS documentation. The Link component in NextJS is not a traditional DOM element, therefore you should assign the className directly to the <a> tag instead of the <Link> component.

A simple example extracted from the documentation:

// pages/index.js
import Link from 'next/link'

function Home() {
  return (
    <>
      <ul>
        <li>Home</li>
        <li>
          <Link href="/about">
            <a>About Us</a>
          </Link>
        </li>
      </ul>
    </>
  )
}

export default Home

Answer №2

import React from 'react';
import Link from 'next/link';
import style from './redButton.module.css';

interface Props {
  href: string;
  text?: string;
}

const RedButtonComponent: React.FC<Props> = ({ href, text }) => {
  return (
    <Link href={href}>
      <a rel="" className={style.redButton}>
        {text}
      </a>
    </Link>
  );
};

export default RedButtonComponent;

To ensure proper styling and behavior, remember to include rel="" and assign a class name with className={} inside the <a> tag rather than in the <Link> component provided by 'next/link'.

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

Enhance user experience by customizing login functionalities in Meteor using React: Integrate personalized

Currently, I am utilizing account-ui to incorporate a drop-down login feature in Meteor. However, I have encountered an issue where I am unable to expand user sign-up functionality to include First and Last Name fields. This is because Meteor account ui on ...

When it comes to TypeScript, it feels like my interface can accept anything I throw at it, and it struggles to grasp how I've implemented and imported redux-toolkit and styled components

My Current Struggle: Errors in Typescript are occurring seemingly at random. The interface in my index.tsx file doesn't align with the object it should describe, yet no red flags are raised. On top of that: An error pops up when attempting to import ...

I am trying to showcase the chosen value in the return section, but unfortunately, I am encountering some difficulties in retrieving the data. It seems to be a minor issue that I am struggling

import { Autocomplete, Stack, TextField } from "@mui/material"; import { Box } from "@mui/system"; import axios from "axios"; import React, { useEffect, useState } from "react"; const SearchProperty = () => { c ...

Working with scrollIntoView in useEffect (Encountering an error trying to access 'scrollIntoView' property of null)

Starting from scratch, I'm working on setting up my webpage and crafting some code. function BasicDetails() { React.useEffect(()=>{ scrollView(); }, []); function scrollView() { document.getElementById('main-root& ...

Jasmine encountered an error while trying to compare the same string: 'Expected the values to match.'

I'm encountering an error message, despite verifying that the strings are identical: Expected { $$state : { status : 1, value : { customerNumber : 'customerNumber', name : 'name', userId : 'buId', customerType : 'ty ...

Tips for modifying an axios instance during response interception

Is there a way to automatically update an axios instance with the latest token received in a response, without making a second request? The new token can be included in any response after any request, and I want to make sure that the last received token ...

Is there a method to implement retries for inconsistent tests with jest-puppeteer?

Currently, I am struggling with an issue where there is no built-in method to retry flaky tests in Jest. My tech stack includes Jest + TypeScript + Puppeteer. Has anyone else encountered this problem or have any suggestions for possible solutions? I attem ...

Dealing with import and export in React using MaterialUI components

Looking to create a component called "Menu" and import the "Menu" from MaterialUI? Here is an example of how you can achieve this: import React, {Component} from 'react'; import Menu from '@material-ui/core/Menu'; class CustomMenu ext ...

Introducing the concept of type-specific name inclusion

I am currently developing my Angular app using TypeScript with the goal of preventing redundancy through some form of generic handling. Here is where I am starting: class BaseProvider { api_url = 'http://localhost:80/api/FILL_OUT_PATH/:id&apo ...

What is the method to store and retrieve data attributes linked to elements such as select options within the React framework?

Despite my efforts, I'm currently unable to retrieve the data attribute from an option using React as it keeps returning null. <select onChange={(e) => this.onIndustryChangeOption(e)} value={this.props.selectedIndustry}> <opti ...

Testing a React component using the `ua-parser-js` plugin with Jest and React Testing Library

I've developed a simple component that displays an image depending on the operating system you are using (in this case, iOS and Android). import { UAParser } from "ua-parser-js"; export const DownloadApp = ({ appleStoreUrl, playStoreUrl }: ...

Enhancing React Native experience by dynamically editing array elements

This code block defines the state of my program, including an array: this.state = { arr: [{ arrid: 0, arrEmail: '', arrName: '', arrPhone: '' }], id: 0, email: "", isChecked: true, name: "", phon ...

I can't seem to get React Fetch and the expressJS post method to cooperate - what could I be overlooking?

I have been diving into the world of React and recently ventured into working with expressJS. However, I encountered an issue with the POST method as the data is not being successfully posted to the server. While I believe my fetching of the post method is ...

What is the best way to add both the id and the full object to an array list at the

Requirements: "admin-on-rest": "^1.3.3", "base64-js": "^1.2.1", "react": "^16.2.0", "react-dom": "^16.2.0" I have a User model that includes a List of Roles. // User { id: "abcd1234", name: "John Doe", ... authorities: [ { ...

The overload functionality in Typescript interfaces is not functioning as intended

Here is a snippet of code I'm working with: class A { } class B { } class C { } interface Builder { build(paramOne: A): string; build(paramOne: B, paramTwo: C): number; } class Test implements Builder { build(a: A) { return &apo ...

Encountering a 'map' error due to undefined properties when attempting to map over an empty array

In the code snippet below, I am mapping data using the map function. However, an issue arises when the arrays are empty, leading to the following error: Cannot read properties of undefined (reading 'map') I want to prevent this error from occur ...

Tips for utilizing ngIf based on the value of a variable

Here is the code from my file.html: <button ion-button item-right> <ion-icon name="md-add-circle" (click)="save();"></ion-icon> </button> The content of file.ts is: editmode = false; I am trying to achieve the foll ...

Tips for verifying React Router route parameters

When using the generatePath function, I encountered an error when not passing all required parameters for a route. const route = ':id/:id2' generatePath(route, { id: 'id' }) Uncaught TypeError: Expected 'id2' to be defined ...

Using Next-auth.js: Redirecting from getServerSideProps to a specific callback URL - How can it be done?

Hey, I've been working on implementing authentication with the NextAuth library in my Next.js application. While following the documentation, I encountered a situation that wasn't covered. I'm attempting to create a 'route guard' o ...

I encountered an issue while attempting to build the react-create-library with Material UI

Recently, I wanted to develop my own React package using the create-react-library command. However, I ran into an issue while following the provided instructions. The error message that popped up when attempting to build my project was as follows: Error: & ...