mobxjs / mobx-react

React bindings for MobX
https://mobx.js.org/react-integration.html
MIT License
4.85k stars 350 forks source link

[Bug?] componentDidUpdate() doesn't trigger after version 6 #692

Closed RosenTomov closed 5 years ago

RosenTomov commented 5 years ago

Like title says, componentDidUpdate() won't trigger since mobx-react version 6.

For example, I want to keep the scroll position to the bottom:

@inject('someStore')
@observer
class SomeList extends React.PureComponent {
...
componentDidMount() {
  this.scrollToBottom();
}

componentDidUpdate() { // never triggers, even though it re-renders, when there's new data
  this.scrollToBottom(); 
}
....

Trying with reaction, doesn't really solve the issue, since it doesn't wait for the element to render and it will scroll to the second to last element instead.

componentDidMount() {
  reaction(
    () => someStore.someArray.length, // or prop, etc..
   (val, currentReaction) => {
     this.scrollToBottom();
     this.scrollbarReactionToDispose = currentReaction; 
     // not sure if I need to dispose reaction in componentWillUnmount
     // since it's in the same class and if it's the correct way to do so
   }
  );
}

componentWillUnmount() {
  this.scrollbarReactionDispose.dispose();
}

I've also tried going back to a React.Component, just to be sure, but no luck. I don't know if I'm missing something.

Thanks.

mweststrate commented 5 years ago

@pavelserbajlo make sure HMR is disabled and that there is only one instance of mobx and mobx-react in your module tree. I've seen both HMR, and improperly organized dependencies as primary usecases for random weird behavior in the past. (if that doesn't help, please take the conversation to a separate issue as indicated by @FredyC, this one is closed now)

winterbe commented 5 years ago

Fixed in 6.1.0

Just wanted to say Thank You for the pragmatic fix. After I had to go back to v4 due to this issue, I've bumped the version back to latest 6.1.0 and everything seems to work like expected. Now my team can adopt hooks iteratively without a hurry or any further migrations. 👏

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.