-
-
Notifications
You must be signed in to change notification settings - Fork 348
Hot reload doesn't work when @observer is used #500
Comments
@urugator tnx, but I used class components and removed decorators, and used observer like this : |
+1 on this. |
cc @theKashey Hi - I'm seeing a similar issue, not resolved by the above suggestions. The mobx dependency graph works correctly, but HMR does not re-render when changing UI. In the example below, both If you switch the Alternatively, if I remove the observer HOC from the Any thoughts? Thanks v much. import React, {Component} from 'react';
import {hot} from 'react-hot-loader';
import {observer} from 'mobx-react';
import {observable} from 'mobx';
class Comp extends Component {
state = {
obsObj: null
};
static getDerivedStateFromProps(nextProps, prevState) {
return {obsObj: nextProps.obsObj};
}
render() {
const {prop1} = this.state.obsObj;
return <div style={{border: '1px solid red'}}>{prop1}</div>;
}
}
const ObsComp = observer(Comp);
class App extends Component {
@observable obj = {
prop1: 'Example'
};
render() {
return (
<div>
<ObsComp obsObj={this.obj}></ObsComp>
</div>
);
}
}
const ObsApp = observer(App);
export default hot(module)(ObsApp); Dependencies: |
@briedel1 - you just spotted one of RHL's limitations. I could understand why it will not work for SFC wrapper by observable (it will not, due to Let me play with it for a while. |
So - there are always just 2 issues:
Anyway - don't touch anything :), I need a broken example to fix. |
Thanks for taking a look. |
So everything is badly broken.
I've tried to "concrete" them, so allocate one symbol per properly name, and it fixes the problem. @mweststrate - is there any sense to use unique symbols to shadow "well-known" props like state and props? |
@theKashey the symbols only need to be unique for the mobx-react module, so as long as the module isn't reloaded, the symbols could stay the same. So I think the symbol created here: https://github.com/mobxjs/mobx-react/blob/master/src/observer.js#L278 could be also build as |
What about just caching them? It |
That would be the neater solution indeed :)
Op do 26 jul. 2018 om 15:51 schreef Anton Korzunov <[email protected]
…:
What about just caching them? It createSymbol could return the same value
for the same keys?
If ok - I'll double check from my side and draft a PR.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#500 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhD7YhyuVnHwo7MsJdTPfOGydD9waks5uKclYgaJpZM4UhOPh>
.
|
Thanks for the help and quick turnaround @theKashey ! |
Released as 5.2.5. Thanks for investigating and fixing @theKashey ! |
|
@reza7rm Our Dependencies: "mobx": "4.5.2", // Thanks to IE
"mobx-react": "5.2.8",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-hot-loader": "~4.3.12", With this setup, I got a nested component (with state, an injected store and @ observer) to update both on store changes (via mobx) and file changes like changing text (via HMR). Our test component: import React, { Component } from 'react'
import { observer, inject } from 'mobx-react';
@inject("permissionStore")
@observer
class Dummy extends Component {
state = {
switchedIcon: false,
}
render() {
let userName = `${this.props.permissionStore.user.givenName} ${this.props.permissionStore.user.surName}`;
return (
<div>
<h2>{userName}</h2>
<button onClick={() => this.setState({ switchedIcon: !this.state.switchedIcon })}>Switch Icon</button>
<FontAwesomeIcon icon={this.state.switchedIcon ? 'minus' : 'plus'} />
</div>
)
}
}
export default Dummy Our App root component is also decorated with @ observer. @observer
class App extends Component {
render() {
return (
<Provider permissionStore={PermissionStore}>
<BrowserRouter>
<Routes /> // The Dummy component is rendered somewhere in here
</BrowserRouter>
</Provider>
);
}
} This setup did not work with <5.2.5 and none of the ~5.3.x releases of mobx-react. But as I said, 5.2.8 works fine. |
Strange thing I noticed over here, experiencing the exact same behavior. MobX observer tag is preventing child components from hot reloading. However - I had the same setup (except using webpack-hot-middleware) working on a totally different project, and started looking for other differences in config.... It turns out that removing the Note: HMR and observer are confirmed working at the root and child level, as expected. Local state is preserved during HMR reload. I did not need to downgrade libraries to fix. Dependencies: Top-level react component (with react-hot-loader setup): Child react component: |
@zfalen Easiest example import 'react'
import 'babel-polyfill'
import 'react-dom';
🔥 react and react-dom got different "Symbol" in IE11, everything is broken PS: please ensure that you have |
@theKashey TLDR I found the issue - dont ever use an arrow function on lifecyle methods (or any other format for that matter. Even if there was a fire) Here are some longwinded tweaking results that may or may not help you or other people experiencing the issue :)
I also tried adding
However, I either totally misreported what I was experiencing at first, or I am on drugs. Testing this morning things started breaking again, which led me down another debug path. As it turns out,
This did not work. Commenting out that line while trying to get things to work again did get HMR working. Voilé! A few more tweaks / experiments later, and I notice:
Some more variations and their results:
Works.
Works. (w/polyfill)
Works. (w/polyfill) Awesome. Seem to have found the issue. Doing weird and unnecessary things to standard React lifecycle methods prevent the component from hot reloading. It was not an issue with MobX
Does Not Work. Although stateless components written with an arrow function do work as expected with HMR:
|
We had an issue with render-as-arrow-function, but, yeah, we never tested componentDidMount-as-arrow-function 🙀 |
I'm still having problems with observer decorator in React Native.
React Native HMR not working when using a decorator like this:
but this works when calling as a funtion:
it also works when using observer decorator with inject decorator like this:
|
@wuifdesign - react-native uses quite old version of RHL, and there is no way for me to help you here. |
@theKashey @mweststrate @eyousefifar Can anyone re-open this issue? Apparently the fix implemented in 5.2.5 broke again in 5.3 >. Just realized there is also this: #559 If it's easier, it would also be helpful if things starting to work again with 6.*. |
package version:
"mobx": "^4.3.1",
"mobx-react": "^5.2.2",
"react": "16.3.1",
"react-native": "0.55.4"
platform: Android
Hi, when I add @ observer to my react class, hot reloading doesn't work. it reloads the code but UI doesn't rerender. it works fine without an observer
I can provide more information if needed
The text was updated successfully, but these errors were encountered: