question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@vitejs/plugin-react-refresh doesn't work with generic react functional components

See original GitHub issue

Describe the bug

Generic typescript react functional components cannot be transformed using plugin-react-refresh.

This works with the typescript compiler.

Reproduction

interface MyProps<T> {
  readonly  example: T;
}
export const MyComponent = <T,>({example}: MyProps<T>) => {
  return null;
}

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 11.0.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 833.76 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.3.0 - /usr/local/bin/node
    npm: 7.0.14 - /usr/local/bin/npm
  Browsers:
    Chrome: 89.0.4389.90
    Edge: 89.0.774.57
    Firefox: 86.0
    Safari: 14.0.1
  npmPackages:
    vite: ^2.1.3 => 2.1.3 

Used package manager:

Logs

10:22:21 AM [vite] hmr update /src/web/components/common/AutoComplete.tsx (x4)
10:22:21 AM [vite] Internal server error: Transform failed with 1 error:
<redacted> 25:6: error: Expected "}" but found ";"
  Plugin: vite:esbuild
  File: <redacted>
  
  Expected "}" but found ";"
  23 |    example
  24 |  }: MyProps<T>) => {
  25 |    _s();
     |        ^
  26 |  

Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
patak-devcommented, Mar 27, 2021

@rockwotj this is an issue with babel TSX parser: https://github.com/babel/babel/issues/12978

It was also reported before here https://github.com/vitejs/vite/issues/2295

There is a workaround for this problem here https://github.com/microsoft/TypeScript/issues/15713#issuecomment-300545248

This is a limitation cased by the use of <T> for generic type parameter declarations. combined with JSX grammar, it makes such components ambiguous to parse.

The workaround is to use a function expression instead:

// Basic example
const paramArray = function <T>(param1: T, param2: T) { return [param1, param2] };

// React specific example
type Component<T> = React.ComponentClass<T> | React.StatelessComponent<T>;
const decorator = function<T>(Component: Component<T>) : Component<T> { return (props) => <Component {...props} /> };
1reaction
rockwotjcommented, Mar 27, 2021

Thank you @patak-js !

Read more comments on GitHub >

github_iconTop Results From Across the Web

@vitejs/plugin-react - npm
The all-in-one Vite plugin for React projects. enable Fast Refresh in development; use the automatic JSX runtime; avoid manual import React in ....
Read more >
How to make a functional React component with generic type?
react native 's Fast Refresh did not work for that component. When the parent component is saved, it reloaded. Does anyone know why...
Read more >
React Javascript - Replit
Here's a template that uses @vitejs/plugin-react and doesn't have errors when adding those two UI packages: replit.com/@phil-mac/React-Vite-Template.
Read more >
jsx-sfc | Yarn - Package Manager
A SFC like React function component API for managing CSS-in-JS and static members. readme. JSX Separate Function Components.
Read more >
react-dom.development.js:18687 the above error occurred in ...
As i can see in your import, You have imported useNavigate from 'react' which is wrong package. Please import from 'react-router-dom';. Open side...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found