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

autobinding lifecycle method #1194

Open
KarolAdamiec opened this issue Feb 28, 2019 · 3 comments
Open

autobinding lifecycle method #1194

KarolAdamiec opened this issue Feb 28, 2019 · 3 comments

Comments

@KarolAdamiec
Copy link

Description

when component has
componentDidMount = () => {}
instead of
componentDidMount(){}

Hot loader identifies files to update and patch, but never rerenders.

Expected behavior

Should rerender, or WARN/LOG/ERROR. At the moment fails silently to reload the app.
At minimum big bold warning in docs....

"react-hot-loader": "4.7.1",
"react": "16.8.1",

@theKashey
Copy link
Collaborator

No changes made to componentDidMount are applied by design - your component was and is mounted already.

@devniel
Copy link

devniel commented Apr 20, 2019

Hi,

I had this same problem, I had to rewrite step by step my class to discover why it's not rerendering, even updated other libraries or things related to #1120, but in the end this is the problem.

It seems that when using componentDidMount = () => {} the component will never be rerendered when changing its content, using the standard componentDidMount(){} fixes it.

Example:

Here, if you change one by two, it will be rerendered:

import React, { Component } from 'react'
import { connect } from 'react-redux';
import { Link, withRouter } from 'react-router-dom';

class Example extends Component {

  componentDidMount(){};

  render() {
    return (
      <div>
        one
      </div>  
    )
  }
}

function mapStateToProps(state) {
  return {};
}

function mapDispatchToProps(dispatch) {
  return {};
}

export default withRouter(
  connect(
    mapStateToProps,
    mapDispatchToProps,
  )(Example),
);

Here, that change will not work:

import React, { Component } from 'react'
import { connect } from 'react-redux';
import { Link, withRouter } from 'react-router-dom';

class Example extends Component {

  componentDidMount = () => {}

  render() {
    return (
      <div>
        one
      </div>  
    )
  }
}

function mapStateToProps(state) {
  return {};
}

function mapDispatchToProps(dispatch) {
  return {};
}

export default withRouter(
  connect(
    mapStateToProps,
    mapDispatchToProps,
  )(Example),
);

I'm using react-hot-loader: 4.8.4 and react: 16.8.6.

Thanks!

@theKashey
Copy link
Collaborator

RHL uses compontDidMount/componentWillUnmount to track their usage -https://github.com/gaearon/react-hot-loader/blob/master/src/proxy/createClassProxy.js#L188

Using "bound" versions of these methods is not letting RHL override them in an expected way, as well as update them in time.

There is no reason to use an arrow version of these methods, no reasons to bind them. Probably the best solution here would be an eslint rule to stop you from doing that.

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

3 participants