mobx icon indicating copy to clipboard operation
mobx copied to clipboard

React.js do not react on MobX changes

Open nikitatrifan opened this issue 9 years ago • 11 comments

Hi guys, I need your help.

ItemsStore

class ItemsStore {
  @observable data = [];
  storeName = config.items;
}

export default new ItemsStore();

Store

import TodoStore from 'stores/TodoStore'
import DeviceStore from 'stores/DeviceStore'
import DomStore from 'stores/DomStore'
import UserStore from 'stores/UserStore'
import ItemsStore from 'stores/ItemsStore'
import FilterStore from 'stores/FilterStore'


export const store = window.store = {
  todo: TodoStore,
  device: DeviceStore,
  dom: DomStore,
  user: UserStore,
  items: ItemsStore,
  filter: FilterStore
};

Entry point

import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'mobx-react'
import { AppContainer } from 'react-hot-loader'
import { App } from 'containers'
import { store } from 'store'

export const Application = (App) => (
  <AppContainer>
    <Provider {...store}>
      <App />
    </Provider>
  </AppContainer>
);

const AppElement = () => document.getElementById('app');

render(
  Application(App),
  AppElement()
);

if (module.hot) {
  module.hot.accept(() => {
    const App = require('./containers/App/App.js').default;

    render(
      Application(App),
      AppElement()
    );
  });
}

Component

import React, { Component } from 'react'
import { observer, inject } from 'mobx-react'
import { ItemTilesBanner } from 'components'
import { devicesBreakpoints } from 'config'


const mapStateToProps = ({device: {width}, items}) => ({
  items, isMobile: width <= devicesBreakpoints.mobile
});

@inject(mapStateToProps) @observer
export default class ItemTilesBannerContainer extends Component {
  componentWillReceiveProps() {
    console.log('ItemTilesBannerContainer will receive props')
  }

  render() {
    const { isMobile, items: {data} } = this.props;
    return <ItemTilesBanner data={data} isMobile={isMobile}/>
  }
}

Trying to change the store mobx

So the data was updated, props of the component too (if I call forceUpdate data will be displayed), but there was no reaction on it (as you see my message from componentWillReceiveProps have not displayed).

nikitatrifan avatar Mar 28 '17 05:03 nikitatrifan

Is ItemTilesBanner an observer?

mweststrate avatar Mar 28 '17 09:03 mweststrate

@mweststrate yeah, I think so -

@observer
export default class ItemTilesBanner extends Component {
...
}

nikitatrifan avatar Mar 28 '17 14:03 nikitatrifan

Too bad that wrap in the observer to the place where the actual data is used, but not where I want. It is abnormal and this will be the first reason for the unpopularity of mobx. The second reason is performance. Performance ten times worse than redux which is all the app re assemble.

acopalipsis avatar Mar 28 '17 16:03 acopalipsis

@acopalipsis

Performance ten times worse than redux

Proof? I just can not image about this case. I made many performance tests with React. Using mobx was all times super-duper performant.

lavrton avatar Mar 28 '17 16:03 lavrton

@lavrton an example of a complex and there is no time to repeat it. If You have a fictional example, it is possible the test was not written correctly. I have an update on the drag and I clearly see how creepy mobx slow moving.for redux, which I'm not very fond of, and, to my surprise this was not.

acopalipsis avatar Mar 28 '17 16:03 acopalipsis

@lavrton bullshit. We don't think so. In some projects redux lose his performance - in some - mobx. Everything should be used by your needs. In the project mobx works as well.

So, any solution for my problem? Should I remove the container and send the data directly to the component?

nikitatrifan avatar Mar 29 '17 03:03 nikitatrifan

@expdevelop, could you create some jsfiddle that reproduces the problem. From what you posted seems that ItemTilesBanner should be updated correctly. (My be useful to start with: https://jsfiddle.net/Sl1v3r/8hngn2tn/)

andykog avatar Apr 01 '17 20:04 andykog

@expdevelop I noticed you are using react-hot-loader. Does turning it of change anything? There are quite some known problems with decorators and RHL.

Otherwise a full reproduction would be much appreciated. As @andykog noted, looking good. Also, did you use the mobx-react-devtools or mobx.whyRun() to see what ItemTilesBanner is actually subscribing to?

mweststrate avatar Apr 03 '17 11:04 mweststrate

Hi @acopalipsis @expdevelop. Communication through the MobX GitHub must be constructive and never resort to personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. In any other open source project I'm a collaborator of would not be nearly as nice as Michel here. In Node, the issue would definitely be locked at this point.

You have provided non-complete code which we can't test or run for a bug you have reported and then proceeded to thumbs-down the library's author @mweststrate. Then you proceeded to make unsubstantiated performance claims without a benchmark and complain about the response you've been getting. (I'm yet to see a single benchmark in which MobX doesn't win - but I'm ready to learn).

MobX data works with or without a container. I've tried to reproduce your example based on https://github.com/Mosho1/react-mobx-boilerplate and have been unsuccessful in getting it to not update.

If you have a bug - please create a Minimal Correct and Verifiable Example which we can use to debug your issue.

benjamingr avatar Apr 03 '17 17:04 benjamingr

In order to prevent these confusions in the future, I have added (with @mweststrate 's blessing) a CoC at https://github.com/mobxjs/mobx/blob/master/CODE_OF_CONDUCT.md

benjamingr avatar Apr 03 '17 17:04 benjamingr

Thanks for everything guys.

I've solved my problem by removing any containers from the project.

nikitatrifan avatar Apr 04 '17 09:04 nikitatrifan