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

extendObservable doesn't help when we need to introduce new properties to the model-it doesn't rerender mobx-react components #194

Closed
capaj opened this issue Apr 15, 2016 · 2 comments

Comments

@capaj
Copy link
Member

capaj commented Apr 15, 2016

I would kind of expect that if I run this small app:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {observable, extendObservable} from 'mobx';
import {observer} from 'mobx-react';
import DevTools from 'mobx-react-devtools';

const obs = observable({})

setTimeout(() => {
  extendObservable(obs, {num: 4}) // doesn't rerender
}, 100)

@observer
class TimerView extends Component {
     render() {
        return (
            <div>
                num: {obs.num}
                <DevTools />
            </div>
        );
     }

};

ReactDOM.render(<TimerView/>, document.getElementById('root'));

that after 100ms it gives me:

num: 4

But it doesn't. It just gives me

num:

Could there be a way to fix this?
Maybe we could have an extendObservable on mobx-react API which would call extendObservable() from mobx and would also rerender any mounted observer component?

If not, maybe just mention in the docs, that extendObservable doesn't really help when your rendering loop is already running and that manual rerender is needed,

BTW, sorry about the complex sample, I was trying out bunch of other things and forgot to reduce it before posting this issue.

@capaj capaj changed the title extendObservable doesn't help when we need to introduce new properties to the model-it doesn't rerender extendObservable doesn't help when we need to introduce new properties to the model-it doesn't rerender mobx-react components Apr 15, 2016
@jbrantly
Copy link

I just ran into a similar issue where I was trying to use an object as a map. I'm not sure if this is relevant in your case, but the observable map worked really well for me.

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {observable, extendObservable, map} from 'mobx';
import {observer} from 'mobx-react';
import DevTools from 'mobx-react-devtools';

const obs = map()

setTimeout(() => {
  obs.set('num', 4)
}, 100)

@observer
class TimerView extends Component {
     render() {
        return (
            <div>
                num: {obs.has('num') ? obs.get('num') : ''}
                <DevTools />
            </div>
        );
     }

};

ReactDOM.render(<TimerView/>, document.getElementById('root'));

The above example is somewhat abusing Map, but it gets the point across.

@mweststrate
Copy link
Member

@jbrantly is right, you cannot observe not yet existing properties and be notified about them, except when using maps.

Note also that if you would have triggered the TimerView by any some other means to re-render after the timeout. Future changes to num will be picked up as the observer list is re-established after each render.

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