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

Warning: setState(...): Can only update a mounted or mounting component #446

Closed
didierfranc opened this issue Dec 9, 2016 · 19 comments
Closed

Comments

@didierfranc
Copy link
Contributor

didierfranc commented Dec 9, 2016

Description

Any idea why I'm getting this when hot reloading happened ?

capture d ecran 2016-12-09 a 15 59 45

Expected behavior

To not have this error

Actual behavior

Everything seems fine but console log errors like that 👆

Environment

React Hot Loader version: 3.0.0-beta.6

Run these commands in the project folder and fill in their results:

yarn && yarn start
  1. Operating system: macOS
  2. Browser and version: Chrome 54

Reproducible Demo

Run the project, editing some code from this https://github.com/didierfranc/redux-react-starter and error appear on stateful components.

@calesce
Copy link
Collaborator

calesce commented Dec 9, 2016

This error is usually caused by #313, but it looks like you have the es2015 preset. I'll try running your project later today and see what's going on.

@didierfranc
Copy link
Contributor Author

didierfranc commented Dec 9, 2016

Solved didierfranc/redux-react-starter@e66ab1f

It seems that render should be equal here

render()

and

module.hot.accept('../src', render)

I did not want to have <AppContainer /> in my prod bundle so I needed to split that, the solution is :

if (!module.hot) render(<Root />, document.querySelector('react'))

at the root of my project, if you have better solution I'll take it !

@ianks
Copy link

ianks commented Jan 6, 2017

@didierfranc I tried this and although the error is gone, hot reloading is not working. Did you experience this at all?

@penx
Copy link

penx commented Sep 12, 2017

I had this issue when trying to switch from babel-preset-es2015 to babel-preset-env. Adding transform-es2015-classes to the included transforms resolved it for me:

presets: [
  ['env', {
    targets: {
      chrome: 60
    },
    include: ['transform-es2015-classes']
  }],
  'stage-1',
  'react'
],
plugins: ['react-hot-loader/babel'] 

@Koenkk
Copy link

Koenkk commented Oct 13, 2017

@penx thanks! It solved the problem in my case. 😄

@edorivai
Copy link

Just leaving a note for fellow Typescript googlers. A TS equivalent of @penx's babel solution would be:

// tsconfig.json
{
	"compilerOptions": {
		"target": "es5"
	}
}

@theKashey
Copy link
Collaborator

The root cause - arrow functions. You have to avoid them yet.

@manishoo
Copy link

manishoo commented Jan 8, 2018

@penx thanks! you saved my week!

@theKashey
Copy link
Collaborator

Everyone - version 4 solves this. Update today.

@vkosovskikh
Copy link

Same problem with version 4.0.0-beta.18 of react-hot-loader. Error occurs when I save some changes on component and change route by clicking a button. Any help?
.babelrc:

presets: [
  ['es2015', { modules: false }],
  'stage-0',
  'react'
],
plugins: [
  'react-hot-loader/babel'
]

rhl_error

@theKashey
Copy link
Collaborator

@vkosovskikh, gonna to fix this in next release.
Keep in mind - this is not an errors, just a warning.

@gregberge
Copy link
Collaborator

@theKashey yes but this is very very annoying. In a big application it is a ton of warnings.

@theKashey
Copy link
Collaborator

One warning for a one component 🤷‍♂️

@theKashey
Copy link
Collaborator

@vkosovskikh - could you try the last beta version?

@ivankoleda
Copy link

@theKashey had the same issue just now with version 4.0.0-beta.21, updating for 4.0.0-beta.22 solved the problem

@vkosovskikh
Copy link

@theKashey seems solved, ty

@Cottin
Copy link

Cottin commented Apr 5, 2018

I've been trying to research this for a while now and I'm still getting this warning. Is it just me?

https://github.com/Cottin/some-boilerplates/tree/master/react-hot-warning
If somebody wants to reproduce, I made a minimal example above.

@theKashey
Copy link
Collaborator

@Cottin - here is an explanation

class Hello extends React.Component {
  constructor(props) {
    super(props)
    this.state = {name: '...'};

    this.getNameAndSet = this.getNameAndSet.bind(this)

    this.getNameAndSet() <-- will be executed on Class(Component) creation
  }
  getNameAndSet() {
    console.log(this)
    setTimeout((() => this.setState({name: 'Worrrld'})), 100) <-- will _always_ be fired
  }

During update RHL trying to undertand wich changes were made, and creating an old and a new class to compare them.
Creating, and throwing away, not adding to the tree.

You shall not trigger state change mechanics in the constructor. Move this logic to componentDidMount.

@Cottin
Copy link

Cottin commented Apr 16, 2018

@theKashey Thanks a lot for explanation! Works great with componentDidMount!

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