When working with SVG files, you may encounter the error message "The JSX element 'Camera' does not have any construct or call signatures."

I'm currently working with nextjs and utilizing the svgr plugin in my nextjs configuration to incorporate svg files as Components. Here is a snippet of my nextjs config:

module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ["@svgr/webpack"],
    });

    return config;
  },
  images: {
    disableStaticImages: true,
  },
};

Within my project, I have stored all my svg files in a directory named icons within public/static so that I can easily import them like this:

import {Camera} from 'public/static/icons'

However, when trying to utilize <Camera/>, I encounter the following error: JSX element type 'Camera' does not have any construct or call signatures.

This appears to be a TypeScript issue, but how can I go about resolving it? I attempted to create a @types folder and include an index.d.ts file with the following content:

declare module "*.svg" {
  const content: any;
  export default content;
}

Unfortunately, this solution did not resolve the problem.

Answer №1

To update the declaration of your module type, make the following adjustments:

declare module "*.svg" {
  import React = require("react");
  export const ReactComponent: React.SFC<React.SVGProps<SVGSVGElement>>;

  const svgUrl: string;
  export default svgUrl;
}

The @svgr/webpack tool converts SVG files into a module containing a default export of type string, along with a ReactComponent property that serves as a directly usable React component.

You can utilize this in two different ways:

import svgUrl, { ReactComponent as Icon } from './icons/Icon.svg'

const MyComponent = () => (
  <>
    <img src={svgUrl} />
    {/* or */}
    <Icon />
  </>
)

Answer №2

After updating to the latest version 11.0.1 and removing the next-dev.d.ts file, the issue was resolved. This is because starting from this version, Next.js will no longer treat SVG files as static files if a custom webpack configuration is detected.

Answer №3

I encountered the same issue and did some digging. It seems that nextjs declares *.svg as StaticImageData in node_modules/next/types/global.d.ts:42. If you manage to redeclare the module type within your project, kindly share your solution.

UPDATE: I made a modification to nextjs like this:

\\ node_modules/next/types/global.d.ts:2
import * as React from 'react';

\\ node_modules/next/types/global.d.ts:43

declare module '*.svg' {
  export const ReactComponent: React.SFC<React.SVGProps<SVGSVGElement>>
  const src: string

  export default src
}

Afterward, I followed the instructions provided on this advice

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

Tips for choosing the correct type of object to use for styling in Vue/Typescript: { }

This is how I implement styling in my Vue TypeScript project. private styleTableObject: CSSStyleSheet = { width: '100%', height: '76%' } I am being forced to use the any type. private styleTableObject: any = { ...

I am looking to apply a loading animation specifically to the ng-select dropdown in an Angular project, without affecting the input field

I have some HTML and TS code that I'd like to share with you. @Component({ selector: 'app-branch', templateUrl: './branch.component.html', styleUrls: ['./branch.component.scss'], animations: [ trigger(' ...

Developing a constrained variable limited to specific values

Recently delving into Typescript - I am interested in creating a variable type that is restricted to specific values. For instance, I have an element where I want to adjust the width based on a "zoom" variable. I would like to define a variable type call ...

Context for Apollo server has not been defined

Following the upgrade to Apollo v4 and migration guide, my project was functioning properly. However, the context is now undefined. const { url } = await startStandaloneServer(server, { listen: { port: 3000 }, context: async ({ req }) => { try ...

Implementing Asynchronous context tracking within a Remix application utilizing Express as the server

Utilizing Remix with Express as the server, I aim to develop an Express middleware that establishes an async context to grant all downstream functions (especially those in the "backend" Remix code) access to this context within the scope of a single reques ...

Achieve the capability to upload multiple files in Next.js using the upload.io integration feature

I'm currently using upload.io for uploads and replicate.com for an AI model on a specific app. I am able to upload one picture, but unfortunately, I am encountering issues when trying to upload multiple pictures. Can anyone identify the problem here? ...

Having trouble with Keycloak authentication in Docker for NextJs app due to ECONNREFUSED error

Struggling with integrating Authentication using Keycloak in my NextJs Application running in Docker Containers. I've set up an API Route to handle the Keycloak login, the page loads properly, but upon clicking the Keycloak Login Button, an error pops ...

Be cautious when combining formik-material-ui TextField and material-ui TextField in the same component as it may result in a warning about the prop `className` not matching

There is a warning that appears when utilizing both formik-material-ui TextField and the original material-ui TextField (which is connected to Formik with the fieldToTextField function) in a single component. The className prop does not match. Server: "P ...

What is the correlation between single-page applications and server-side rendering?

My issue lies in grasping the concept of how server-side rendering single-page application frameworks like Next.js are able to receive pre-rendered full HTML on the front end without having to completely rewrite the entire page. The Next.js website explain ...

What is the correct way to invoke a method from a generic in TypeScript?

My goal is to develop a function that invokes a specific method on a generic object. Here's my initial attempt: type MethodName<S extends Object, M extends keyof S> = S[M] extends Function ? M : never const callMethod = <S, M extends keyof ...

When accessing the innerHTML and outerHTML properties on an HTMLElement, they may return undefined without triggering

Task: My goal is to take an HTML string, make changes to certain attributes of image tags within it, and then return the modified HTML string. The function I have developed follows these steps: private resolveImagesInHTML (body: string): string { le ...

Guide to utilizing JWT stored within a cookie in NEXT middleware

This question is a continuation from my previous inquiry on Stack Overflow. It pertains to rebuilding an application in Next.js that includes a login form utilizing JWT tokens. In the previous version of the app, I stored the access token in memory using ...

The return type of a getter is `any` if the object contains a method and is processed by a generic function

I am facing an issue with my code where the getter's return type is set to any, even though the actual return type should be clear. There are certain additional functions triggering this behavior: // This is necessary for reproduction const wrapperFun ...

Exploring Anchor Tags in Next.js

I'm having trouble using an anchor tag in Next.js Despite not receiving any console errors when setting it up and clicking the link, the page doesn't scroll to the specified id tag. A recent GitHub issue implies that a significant amount of cus ...

Creating a personalized directive in Angular 2 that restricts input to only characters that adhere to a specified regular expression

I have successfully developed a directive that restricts any character from being typed in an input field if it does not match a specified pattern. import { Directive, Input, Output, HostListener, EventEmitter } from "@angular/core" @Directive({ select ...

What is the process for setting up a Quill Editor within an Angular 2 Component?

I am currently working on creating my own Quill editor component for my Angular 2 project. To integrate Quill into my project, I utilized npm for installation. My goal is to develop a word counter application using this component and I am referring to the ...

Employing setState in an arrow function reinitializes the state

My goal is to update a state whenever a user changes input fields on my dashboard. Here's how the handler should operate: If the state is empty, create a user with standard values and change them to the new inputs If the user already exists in t ...

Utilize the Typescript model in the form of an Array structure

I have created some models and I need to use the type that will be assigned as an array. After receiving a result from an api call, I instantiate a new object of my 'type', but the result is an array. How can I make this work? When I set the var ...

What purpose does a cast serve when used on a return type that is defined generically?

Consider this straightforward property access function export function accessProperty<T, K extends keyof T, P extends T[K]>(name: K, v: T): P { return v[name] as P } What is the significance of the cast as P in this context? I experimented with ...

Filtering React Tables with Next.js

I am encountering an issue with the filtering functionality of a component that utilizes React Table. Despite inputting a search term into the designated field, the filtering process seems to initiate momentarily but then nothing happens. I have simply cop ...