question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mobx Invalidating render even though observable object has not changed( only applies to versions < 5.0), the same works with Maps though.

See original GitHub issue

I have a dynamic Keyed Observable object and I use the set and get Object API(s). Please have a look at my code

const{observable,autorun,get,set,observe}=mobx
const{observer}=mobxReact

const store=observable({
  mapping:{}
})

const Test = observer((props)=>{
  console.log(`Rendering ${props.item}`)
  mobx.trace()
 return <li>{get(store.mapping,`${props.item}`)&&get(store.mapping,`${props.item}`).value}</li>
})

ReactDOM.render(<div>{[1,2,3,4,5].map(item=><Test item={item} key={item}/>)}</div>,document.getElementById("app"))

Console output for MobX 5

"Rendering 1"
"[mobx.trace] '_class#3.render()' tracing enabled"
"Rendering 2"
"[mobx.trace] '_class#4.render()' tracing enabled"
"Rendering 3"
"[mobx.trace] '_class#5.render()' tracing enabled"
"Rendering 4"
"[mobx.trace] '_class#6.render()' tracing enabled"
"Rendering 5"
"[mobx.trace] '_class#7.render()' tracing enabled"
set(store.mapping,"5",{["value"]:89})
"[mobx.trace] '_class#7.render()' is invalidated due to a change in: 'ObservableObject@1.mapping.5?'"
"Rendering 5"
undefined
set(store.mapping,"2",{["value"]:89})
"[mobx.trace] '_class#4.render()' is invalidated due to a change in: 'ObservableObject@1.mapping.2?'"
"Rendering 2"
undefined

In Mobx 5 when new keys are added it re-renders only those elements which have subscribed to that key ie… 5 and 2 in my case.

Now take a look at Mobx 4.5.2 output for the same code

"Rendering 1"
"[mobx.trace] '_class#3.render()' tracing enabled"
"Rendering 2"
"[mobx.trace] '_class#4.render()' tracing enabled"
"Rendering 3"
"[mobx.trace] '_class#5.render()' tracing enabled"
"Rendering 4"
"[mobx.trace] '_class#6.render()' tracing enabled"
"Rendering 5"
"[mobx.trace] '_class#7.render()' tracing enabled"
set(store.mapping,"2",{["value"]:89})
"[mobx.trace] '_class#7.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#6.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#5.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#4.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#3.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"Rendering 1"
"Rendering 2"
"Rendering 3"
"Rendering 4"
"Rendering 5"
undefined
set(store.mapping,"5",{["value"]:89})
"[mobx.trace] '_class#7.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#6.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#5.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#4.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"[mobx.trace] '_class#3.render()' is invalidated due to a change in: 'keys(ObservableObject@1.mapping)'"
"Rendering 1"
"Rendering 2"
"Rendering 3"
"Rendering 4"
"Rendering 5"
undefined

Mobx re-renders all the elements wether or not they are subscribed to it and also the message printed by trace is different for both Versions.

Is this a limitation of mobx versions below 5.0.0 due to lack of proxying or is this intended behaviour

BTW i’m setting the store directly in my console window on JSBin

Thank you

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mayorovpcommented, Nov 7, 2018

Bug (?) lives here:

function has$1(obj, key) {
    if (isObservableObject(obj)) {
        // return keys(obj).indexOf(key) >= 0
        var adm = getAdministration(obj);
        adm.getKeys(); // make sure we get notified of key changes, but for performance, use the values map to look up existence
        return !!adm.values[key];
    }

function get(obj, key) {
    if (!has$1(obj, key)) 
        return undefined;
0reactions
lock[bot]commented, Jul 21, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MobX API Reference
Supports observable arrays, objects, Maps and primitives. It does NOT recurse into non-observables, these are left as they are, even if they contain...
Read more >
Observe changes in dynamically keyed objects with MobX ...
Let's add an event handler that mutates the Observable Map but will still let the component re-render: This event handler will first check...
Read more >
Why Mobx observer is not triggering re-render on React ...
I am trying to create a recursive component that stores it's state in a mobx observable object. For some peculiar ...
Read more >
mobx-react - npm
state will be made observables, so the component will react to all changes in props and state that are used by render ....
Read more >
MobX Quick Start Guide
This book will not just guide you through the basics; it will also immerse you in ... works the same way as observable.array()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found