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

Hot reloading "fat arrow function" in component not possible #221

Closed
adesmet opened this issue Dec 29, 2015 · 4 comments
Closed

Hot reloading "fat arrow function" in component not possible #221

adesmet opened this issue Dec 29, 2015 · 4 comments

Comments

@adesmet
Copy link

adesmet commented Dec 29, 2015

This code works fine with hot reloading:

export default class App extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = { counter: 1 };
  }

  render() {
    return (
      <div>
        <h1>{this.state.counter}</h1>
        <a onClick={(e) => {
          this.setState({counter:this.state.counter-1})
        }}>Increment</a>
      </div>
    );
  }
}

But when I extract the handler into a variable, it still works functionally but it doesn't hot-reload:

export default class App extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = { counter: 1 };
  }

  increment = (e) => {
    this.setState({counter:this.state.counter+1})
  }

  render() {
    return (
      <div>
        <h1>{this.state.counter}</h1>
        <a onClick={this.increment}>Increment</a>
      </div>
    );
  }
}

Any thoughts on this?

@gaearon
Copy link
Owner

gaearon commented Dec 29, 2015

This is not currently supported and in fact unlikely to ever be supported in React Hot Loader because it transpiles to an assignment inside a constructor. We might have a solution based on Babel which will handle this, in the future.

@gaearon gaearon closed this as completed Dec 29, 2015
@eisisig
Copy link

eisisig commented May 20, 2016

Sorry for posting in closed issue.

I guess this is still relevant to v3? Sadly 1. we have a lot of state in UI components 2. We use the arrow way everywhere

Was testing upgrade to v3

@gaearon
Copy link
Owner

gaearon commented May 20, 2016

@eisisig Feel free to help out! #242.

@tyleralves
Copy link

I know this is old, but I've established what I think of as an acceptable workflow for using fat arrow click handlers with hot reload.

Basically just make a copy of the click handler within your render function, and replace the onClick with that copy. Make changes and enjoy the hot reload goodness, then go back and modify original class property.

import * as React from 'react';

const styles = require('./styles.css');

export class MainContainer extends React.Component<any, any> {
  constructor() {
    super();
    this.state = {
      name: 'Tyler'
    };
  }
  // Make sure to go back and modify this
  handleDivClick = () => {
    console.log('click!');
    this.setState({
      name: 'John'
    });
  };

  render() {
    // Temporary hot-reload copy
    const handleDivClick1 = () => {
      console.log('click!');
      this.setState({
        name: 'TyTy'
      });
    };
    return (
      // Replace onClick with copy
      <div className={styles.color} onClick={handleDivClick1}>{`Name: ${this.state.name}`}</div>
    );
  }
}

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

4 participants