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

Abnormal behavior with arrow function class methods caused by RHL #428

Closed
nakamorichi opened this issue Nov 25, 2016 · 4 comments
Closed

Comments

@nakamorichi
Copy link

Problem: RHL causes Components with arrow function class methods to behave abnormally.

First, here's my .babelrc:

{
	"presets": ["stage-2", "react"],
	"env": {
		"development": {
			"plugins": ["react-hot-loader/babel"]
		}
	}
}

, and here's the code:

import React, { Component } from 'react';

export default class ExampleModal extends Component {
	show = () => {
		console.log('The function gets called as expected but setState gives warning about unmounted component');
		this.setState({ is_shown: true });
	}

	render() {
		return (
			<button onClick={this.show}>Press me</button>
		);
	}
}

I found two fixes: either remove "react-hot-loader/babel" from Babel plugins, or changing show from arrow class method to ordinary class method and adding manual binding of this:

	show() {
		console.log('The function is executed without issues');
		this.setState({ is_shown: true });
	}

	render() {
		return (
			<button onClick={this.show.bind(this)}>Press me</button>
		);
	}

I originally posted the issue to StackOverflow, but after realizing the connection to RHL, I figured out this is more approriate place to find answers.

So, is the behavior a bug of RHL, because it does seem like so? How about fix?

I have an example app that can be used for reproducing the issue. Just embed the component above for example into index.jsx.

@calesce
Copy link
Collaborator

calesce commented Nov 25, 2016

I think this is another instance of #313. Try adding either babel-preset-es2015 or just transform-es2015-classes.

@nakamorichi
Copy link
Author

That means transpiling ES6 to ES5, which seems like a temporary fix, as there shouldn't be need for such transpilation, although it doesn't matter much either as it happens only in dev mode.

I was able to get the code working by adding transform-class-properties and transform-es2015-classes (the plugins have to be in correct loading order also):

{
	"presets": ["stage-2", "react"],
	"env": {
		"development": {
			"plugins": [
				"transform-class-properties",
				"transform-es2015-classes",
				"react-hot-loader/babel"
			]
		}
	}
}

@calesce
Copy link
Collaborator

calesce commented Nov 25, 2016

That means transpiling ES6 to ES5, which seems like a temporary fix, as there shouldn't be need for such transpilation

Right, that's why it's an open issue, and adding the transform is a workaround. 😉

@nakamorichi
Copy link
Author

Anyway, thanks for quick reply. Spent so many hours today trying to find solution. Also I don't mind the workaround as it doesn't affect the production bundle. Hope you can find solution (the issue seems to have been open already 4 months ;))

nakamorichi added a commit to nakamorichi/react-webpack-babel-hotreload-example that referenced this issue Nov 25, 2016
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

No branches or pull requests

2 participants