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.

[TypeScript] Refresh not working with arrow functions

See original GitHub issue

Hey There,

I’m running into issues getting hot reloading working in my webpack-typescript app:

image

I’m only getting this error when I use arrow functions to declare my React components:

// hot-reload doesn't work
const App = () => {
  return ....
}
export default App

// hot-reload works
function App () {
  return ...
}
export default App;

This approach works when I use the sample project though.

https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/217

 plugins: [
    new ReactRefreshPlugin(),
    new ForkTsCheckerWebpackPlugin(),
    new HtmlWebpackPlugin({
      filename: './index.html',
      template: './public/index.html',
    }),
  ]

Dependencies:

"fork-ts-checker-webpack-plugin": "^6.0.6",
"html-webpack-inline-source-plugin": "^0.0.10",
"html-webpack-plugin": "^4.4.1",
"react-refresh": "^0.9.0",
"react-refresh-typescript": "^1.1.0",
"style-loader": "^0.23.1",
"ts-loader": "^8.0.4",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0",

Any ideas on what is causing this issue?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Jack-Workscommented, May 23, 2021

I tried to reproduce this and found it’s likely not a bug in react-refresh-typescript.

If used as entry component means the following code:

import { render } from 'react-dom'
import { Component } from './Component'

export function App() {
    return (
        <div>
            <Component />
        </div>
    )
}
render(<App />, document.getElementById('root')!)

Yes, in this case, it won’t refresh but reload the whole tree. Because every time this file getting HMRed, this whole file will be re-executed. And if you re-call ReactDOM.render (the last line) on your root component, it will be unmounted and remounted, because the previous App has a different identity than the current App, and the reconciliation algorithms will not diff the vDOM tree for a different component tree.

If it is your case, please move App to another file, that will fix the problem. If not, please point out and give a reproduce. Thanks

1reaction
Ogg70commented, May 21, 2021

Hi, I have exactly the same problem, but I have come to the conclusion that the arrow functions do not have to do with it. As far as I understand, it’s all about the use of hooks.

// hot-reload works
// displayName in React Developer Tools - App
const App = () => {
  return <div><button>add</button></div>;
};

export default App;
// hot-reload doesn't work(triggered full reload)
// displayName in React Developer Tools - Anonymous
const App = () => {
  const [count, setCount] = React.useState(1)
  return <div><button onClick={() => setCount(count + 1)}>add</button></div>;
};

export default App;

I also noticed that in React Developer Tools, the arrow function components are named <Anonymous>. Most likely the problem is in react-refresh-typescript, for some reason the component loses displayName.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Anonymous arrow functions cause Fast Refresh to not ...
I have this code in my pages/index.js file: import React from 'react'; import dynamic from ...
Read more >
Anonymous arrow functions cause Fast Refresh to ... - GitHub
When running the example with npm run dev The following warning is displayed in the console: Anonymous arrow functions cause Fast Refresh to ......
Read more >
Basic Features: Fast Refresh - Next.js
Next.js' Fast Refresh is a new hot reloading experience that gives you ... arrow functions like export default () => <div />; cause...
Read more >
Arrow Function Snippets - Visual Studio Marketplace
The arrow function allows to accomplish the same result with fewer lines of code and approximately half the typing. Curly brackets aren't required...
Read more >
Use ES6 Arrow Functions to Resolve "TypeError - Pluralsight
To do this, you can use ES6 arrow functions. Apart from making your code more succinct and readable, arrow functions serve another purpose....
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