When using typescript and react-hot-loader, component handlers are not being patched correctly after hot reloading.
Please have a look at https://github.com/llamadeus/parcel-typescript-react-hot-loader for a minimal setup.
.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"react-hot-loader/babel",
"@babel/plugin-proposal-class-properties"
]
}
package.json
{
"name": "parcel-react-hot-loader-test",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"start": "parcel src/index.html"
},
"devDependencies": {
"@babel/core": "^7.0.0-0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/preset-react": "^7.0.0",
"parcel-bundler": "^1.11.0",
"typescript": "^3.2.4"
},
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-hot-loader": "^4.6.3"
}
}
When I hit the Press me button for the first time, it should properly alert('test');. Then, after commenting line 9 in src/Content.tsx and waiting for HMR to update the component, another click on the button should NOT trigger an alert.
The second button press actually DOES trigger another alert.
No idea.
Just any typescript application using hot module replacement.
https://github.com/llamadeus/parcel-typescript-react-hot-loader
| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.11.0 |
| Node | 8.12.0 |
| npm/Yarn | 6.4.1/1.12.3 |
| Operating System | macOS 10.14.2 |
The same problem appears, when using ES6 and class decorators.
As it turns out, the bug only exists for arrow functions in the class. Replacing the arrow function with a normal class method and binding this manually works. However, I prefer arrow functions, so I'd like to be able to avoid that.
Can confirm! Thanks, @llamadeus.
Looks like I'll just do a codemod later.
Any news on this? Otherwise, I'd appreciate any tips on where and how to fix this problem.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.
Most helpful comment
Any news on this? Otherwise, I'd appreciate any tips on where and how to fix this problem.