Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS Error: SnackbarProvider cannot be used as a JSX component #485

Closed
ryancrunchi opened this issue Apr 27, 2022 · 40 comments · Fixed by #519
Closed

TS Error: SnackbarProvider cannot be used as a JSX component #485

ryancrunchi opened this issue Apr 27, 2022 · 40 comments · Fixed by #519

Comments

@ryancrunchi
Copy link

Seems there is a conflict in types between @types versions used. I use "@types/react": "^18.0.8" and "@types/react-dom": "^18.0.0" while notistack and/or mui use "@types/react": "^16.8.6 || ^17.0.0" or "@types/react": "^16.9.23"

Expected Behavior

No compiler error

Current Behavior

Compiler error:

'SnackbarProvider' cannot be used as a JSX component.
  Its instance type 'SnackbarProvider' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'React.ReactNode' is not assignable to type 'import("node_modules/@mui/types/node_modules/@types/react/index").ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.
          Type '{}' is missing the following properties from type 'ReactPortal': key, children, type, props

Steps to Reproduce

Link:

Context

Your Environment

Tech Version
Notistack v2.0.4
React ^18.1.0
@types/react ^18.0.8
@types/react-dom ^18.0.0
@manfe
Copy link

manfe commented Apr 29, 2022

Same here with [email protected] and @types/[email protected]

@luisr-carrillo
Copy link

Same here with [email protected] and @types/[email protected]. Hope the PR can fix it. 🙏

@NHarviel
Copy link

NHarviel commented May 6, 2022

I was getting this same compile error I updated my @types/react to 18.0.8 and I am no longer getting the compilation error.

@ryancrunchi
Copy link
Author

I solved by setting in package.json

  "resolutions": {
    "@types/react": "18.0.8",
    "@types/react-dom": "18.0.3"
  }

@luisr-carrillo
Copy link

I solved by setting in package.json

  "resolutions": {
    "@types/react": "18.0.8",
    "@types/react-dom": "18.0.3"
  }

It works with that config. Thank you! 🙏

@iamhosseindhv
Copy link
Owner

Thanks for reporting and I'm glad there's some solution for this. I've been unable to reproduce, so if you could provide one, I'll reopen this issue.

@iamhosseindhv iamhosseindhv reopened this May 16, 2022
@iamhosseindhv iamhosseindhv changed the title Typescript @types/react 18 - 'SnackbarProvider' cannot be used as a JSX component TS Error: SnackbarProvider cannot be used as a JSX component May 16, 2022
@iamhosseindhv
Copy link
Owner

As mentioned above, I'd need a reproduction sandbox to investigate this

@hangaoke1
Copy link

/node_modules/notistack/dist/index.d.ts

export class SnackbarProvider extends React.Component<SnackbarProviderProps> {
    enqueueSnackbar: ProviderContext['enqueueSnackbar'];

    closeSnackbar: ProviderContext['closeSnackbar'];
}

there is no render function in declaration file

export class SnackbarProvider extends React.Component<SnackbarProviderProps> {
    enqueueSnackbar: ProviderContext['enqueueSnackbar'];

    closeSnackbar: ProviderContext['closeSnackbar'];

    render(): ReactNode;
}

fix the problem

the reason is when you extends React.Compoent there is no render function declaration
image

@LorhanSohaky
Copy link

Same error

@outragedhuman
Copy link

Same error with:

"react": "^17.0.2",
"@types/react": "^17.0.35",

Adding the suggested resolutions to package.json breaks my app in other ways and doesn't resolve the TS error.

FWIW this was never working for me, I just tried adding notistack to my app Today. I also just tried replicating in Codesandbox but it was struggling to find react-dom 17.0.2 so I didn't get anywhere.

If I manually patch /node_modules/notistack/dist/index.d.ts as described by @hangaoke1, it works. Is it possible to get a fix for that? Since I can't get a Codesandbox working I could provide you with a test branch from my app if that would help?

@bibixx
Copy link

bibixx commented Jun 1, 2022

I am currently facing the same issue using react 17.0.2. I have tried to reproduce it in CodeSandbox but the issue didn't appear there! From my small investigation it seems like the issue is yarn.
@iamhosseindhv I have created a reproduction repository bibixx/notistack-issue-485 that uses yarn (also there's a branch using npm where the whole thing seems to work just fine)

For the quick patch you can create a .d.ts file (like notistack.d.ts) and augment the module with:

import { SnackbarProvider as SnackbarProviderBase } from 'notistack';

declare module 'notistack' {
  export interface SnackbarProvider extends SnackbarProviderBase {
    render(): ReactNode;
  }
}

P.S. I believe CodeSandbox uses npm underneath so that's why we couldn't reproduce it there

@outragedhuman
Copy link

I can confirm my affected project uses yarn.

@dryhurst
Copy link

dryhurst commented Jul 5, 2022

also using yarn. also react 17.0.2

i was able to fix the issue by making sure the following packages were aligned:

react, @types/react, and @types/react-dom

all set to version 17.0.2

i also created the file notistack.d.ts and put declare module 'notistack'; in the file.

and as if by magic, the issue resolved.

not sure if this is the preferred solution but it worked for me, for now.

@gjoseph
Copy link

gjoseph commented Jul 20, 2022

also using yarn. also react 17.0.2

i was able to fix the issue by making sure the following packages were aligned:

react, @types/react, and @types/react-dom

all set to version 17.0.2

i also created the file notistack.d.ts and put declare module 'notistack'; in the file.

and as if by magic, the issue resolved.

not sure if this is the preferred solution but it worked for me, for now.

that does nothing but override the type declaration of notistack with an empty one.

The real fix is highlighted by @hangaoke1 above -- which is something you can do in your local notistack.d.ts too, but ideally it should be fixed upstream in this package

gjoseph added a commit to gjoseph/Tichu that referenced this issue Jul 21, 2022
@Rich79923
Copy link

Any update for when this will be fixed?

@C0DESTR0NG
Copy link

Any update for when this will be fixed?

I also need a fix for this. Following!

@w90
Copy link

w90 commented Aug 25, 2022

For Typescript and React users, a possible solution:

import providers, { Provide } from 'react-manage-providers'
import { SnackbarProvider } from 'notistack'
import { SNACKBAR_OPTIONS, QUERY_CLIENT_OPTIONS, QueryClientProvider } from '@company/utils-general'

providers.push(SnackbarProvider, SNACKBAR_OPTIONS)
providers.push(QueryClientProvider, QUERY_CLIENT_OPTIONS)

export const MasterProvider = Provide
export const App = () => (
  <MasterProvider>
    <CssBaseline />

    <BrowserRouter>
      <RootRouter />
    </BrowserRouter>
  </MasterProvider>
)

@vwalsh vwalsh mentioned this issue Sep 5, 2022
@kevo1ution
Copy link

kevo1ution commented Oct 5, 2022

I'm using:

    "react": "17.0.2",
    "react-dom": "17.0.2",

Downgrading to

    "@types/react": "^17.0.39",

and deleting my package-lock.json and node_modules folder fixed the problem for me

@iamhosseindhv
Copy link
Owner

The fix for this is published as v2.0.7. Sorry for the tardy fix. Appreciate if you could confirm the fix has resolved the issue.

@jordaaash
Copy link
Contributor

Just updated to 2.0.7 and getting some new errors:

node_modules/.pnpm/[email protected]_o5fu5zzztezljyppmu7zljxb6q/node_modules/notistack/dist/index.d.ts:263:15 - error TS2304: Cannot find name 'ReactNode'.

263     render(): ReactNode;
                  ~~~~~~~~~

node_modules/.pnpm/[email protected]_o5fu5zzztezljyppmu7zljxb6q/node_modules/notistack/dist/index.d.ts:274:76 - error TS2724: 'global.JSX' has no exported member named 'element'. Did you mean 'Element'?

274 export declare const SnackbarContent: (props: SnackbarContentProps) => JSX.element

Resolved dependencies:

react        = 18.2.0
@types/react = 18.0.21

@jordaaash
Copy link
Contributor

First error is because this line references a nonexistant import:

render(): ReactNode;

This should be React.ReactNode consistent with other references.

@jordaaash
Copy link
Contributor

And the second type JSX.element should maybe be React.ReactElement?

@jordaaash
Copy link
Contributor

PR to fix: #520

@iamhosseindhv
Copy link
Owner

The fix is published as v2.0.8.

@outragedhuman
Copy link

The fix is published as v2.0.8.

Should this fix resolve the issue for React 17, or just React 18?

I'm running 17.0.2 and still seeing this on Notistack 2.0.8:

"react": "^17.0.2",
"notistack": "^2.0.8",
 ERROR(TypeScript)  'SnackbarProvider' cannot be used as a JSX component.
  Its instance type 'SnackbarProvider' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'React.ReactNode' is not assignable to type 'import("/Users/nbowers/git/upnorthcoding/projectJovian/node_modules/@types/react-transition-group/node_modules/@types/react/index").ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.
 FILE  /Users/nbowers/git/upnorthcoding/projectJovian/src/index.tsx:14:6

    12 |     <CssBaseline />
    13 |     {/* TODO: Is it possible to lower this in the app's heirarchy? It could lead to less un-neccesary renders */}
  > 14 |     <SnackbarProvider maxSnack={MaximumSnackbarMessages}>
       |      ^^^^^^^^^^^^^^^^
    15 |       <App />
    16 |     </SnackbarProvider>
    17 |   </React.StrictMode>,

[TypeScript] Found 1 error. Watching for file changes.

@EvanVujcec
Copy link

The fix is published as v2.0.8.

Should this fix resolve the issue for React 17, or just React 18?

I'm running 17.0.2 and still seeing this on Notistack 2.0.8:

"react": "^17.0.2",
"notistack": "^2.0.8",
 ERROR(TypeScript)  'SnackbarProvider' cannot be used as a JSX component.
  Its instance type 'SnackbarProvider' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'React.ReactNode' is not assignable to type 'import("/Users/nbowers/git/upnorthcoding/projectJovian/node_modules/@types/react-transition-group/node_modules/@types/react/index").ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.
 FILE  /Users/nbowers/git/upnorthcoding/projectJovian/src/index.tsx:14:6

    12 |     <CssBaseline />
    13 |     {/* TODO: Is it possible to lower this in the app's heirarchy? It could lead to less un-neccesary renders */}
  > 14 |     <SnackbarProvider maxSnack={MaximumSnackbarMessages}>
       |      ^^^^^^^^^^^^^^^^
    15 |       <App />
    16 |     </SnackbarProvider>
    17 |   </React.StrictMode>,

[TypeScript] Found 1 error. Watching for file changes.

I'm also having this issue on React 17.0.1 (environment on this project does not allow higher than 17.0.1)
Upgrading the types to 18 fixes the build error but still shows a type error in editor which can be suppressed 😐.

@kidqn
Copy link

kidqn commented Nov 4, 2022

The fix is published as v2.0.8.

Should this fix resolve the issue for React 17, or just React 18?
I'm running 17.0.2 and still seeing this on Notistack 2.0.8:

"react": "^17.0.2",
"notistack": "^2.0.8",
 ERROR(TypeScript)  'SnackbarProvider' cannot be used as a JSX component.
  Its instance type 'SnackbarProvider' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'React.ReactNode' is not assignable to type 'import("/Users/nbowers/git/upnorthcoding/projectJovian/node_modules/@types/react-transition-group/node_modules/@types/react/index").ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.
 FILE  /Users/nbowers/git/upnorthcoding/projectJovian/src/index.tsx:14:6

    12 |     <CssBaseline />
    13 |     {/* TODO: Is it possible to lower this in the app's heirarchy? It could lead to less un-neccesary renders */}
  > 14 |     <SnackbarProvider maxSnack={MaximumSnackbarMessages}>
       |      ^^^^^^^^^^^^^^^^
    15 |       <App />
    16 |     </SnackbarProvider>
    17 |   </React.StrictMode>,

[TypeScript] Found 1 error. Watching for file changes.

I'm also having this issue on React 17.0.1 (environment on this project does not allow higher than 17.0.1) Upgrading the types to 18 fixes the build error but still shows a type error in editor which can be suppressed 😐.

Please upgrade React to 17.0.2

add declare module 'notistack' to react-app-env.ts

package.json:

  • "react": "^17.0.2",
  • "notistack": "2.0.8",
  • "react-dom": "^17.0.2"
  • "@types/react": "^17.0.52"
  • "@types/react-dom": "^17.0.18"

@garrett-wombat
Copy link

Im having the same issue after updating to react 18.2.0 and react-dom 18.2.0.

I had to just just ignore the ts error

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

Has anyone found a solution to this?

@chrishawn
Copy link

yeah this isnt fixed in React 17.02 and notistack 2.0.8 using yarn at least

@chrishawn
Copy link

so the solution seems to be update to latest notistack 2.0.8
then do

    "@types/react": "^18.0.26",
    "@types/react-dom": "^18.0.9",

even if you leave these at 17

   "react": "^17.0.2",
   "react-dom": "^17.0.2",

@raimuhammad26
Copy link

so the solution seems to be update to latest notistack 2.0.8 then do

    "@types/react": "^18.0.26",
    "@types/react-dom": "^18.0.9",

even if you leave these at 17

   "react": "^17.0.2",
   "react-dom": "^17.0.2",

Saved my life. Thank you.

@ninjarogue
Copy link

ninjarogue commented May 22, 2023

We're having the same issue here as well...

    "notistack": "3.0.1",

    "react": "18.2.0",
    "react-dom": "18.2.0",

    "@types/react": "18.2.6",
    "@types/react-dom": "18.2.4",
    
    "packageManager": "[email protected]"

Is there a fix for this yet?

@AlbinoGeek
Copy link

This still happens.

@aprilmintacpineda
Copy link

Just encountered this with:

"@types/react": "18.2.8",
"@types/react-dom": "18.2.4"

@AlbinoGeek
Copy link

In my case, it was fixed by deleting yarn.lock and doing yarn install again. Something got funky with the versions after a merge messed with the lockfile.

@nouman91
Copy link

I am getting this issue as well. I tried the @bibixx solution but unfortunately, it's not working for me.

@nouman91
Copy link

Any help would definitely be appreciated in this. My dependencies:

react: 17.0.2 react-redux: 17.0.2 noistack: 3.0.1

@ammoradi
Copy link

ammoradi commented Sep 4, 2023

Same issue here:

"@types/react": "^18.2.12",
"@types/react-dom": "^18.2.5",

"notistack": "3.0.1",

Note: I'm using npm v9.8.0

@AdedigbaOluwad1
Copy link

AdedigbaOluwad1 commented Sep 4, 2023 via email

@ammoradi
Copy link

ammoradi commented Sep 4, 2023

I am currently facing the same issue using react 17.0.2. I have tried to reproduce it in CodeSandbox but the issue didn't appear there! From my small investigation it seems like the issue is yarn. @iamhosseindhv I have created a reproduction repository bibixx/notistack-issue-485 that uses yarn (also there's a branch using npm where the whole thing seems to work just fine)

For the quick patch you can create a .d.ts file (like notistack.d.ts) and augment the module with:

import { SnackbarProvider as SnackbarProviderBase } from 'notistack';

declare module 'notistack' {
  export interface SnackbarProvider extends SnackbarProviderBase {
    render(): ReactNode;
  }
}

P.S. I believe CodeSandbox uses npm underneath so that's why we couldn't reproduce it there

I could fix the issue for my case in a bit different way:

import { Component, ReactNode } from 'react';
import { SnackbarProviderProps } from 'notistack';

declare module 'notistack' {
  export interface SnackbarProvider extends Component<SnackbarProviderProps> {
    render(): ReactNode;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.