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.

observable.map.replace doesn't seem to work for [[k,v], [k,v]] form

See original GitHub issue

Welcome to MobX. Please provide as much relevant information as possible!

I have a: 3. [ ✔️] Issue:

  • Elaborate on your issue. What behavior did you expect?

          `
              const newMap = [['a', 'hi'], ['b', 'hello']]
     
              @action setMyMap(myNewMap) {
                  if(this.myMap)
                        this.myMap.replace(myMap) //This is not triggering re-render
                  else
                        this.myMap = observable.map(myNewMap)
               }
    
               this.setMyMap(newMap);
          `
        
           It started working when instead of    `this.myMap.replace(myMap)`, I did a
           `
                this.myMap.clear()
                this.myMap.merge(myNewMap)
           `
    
  • State the versions of MobX and relevant libraries. Which browser / node / … version?

    MobX - 5.7.0

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
CubedEyecommented, Jan 16, 2019

@mweststrate Can confirm this is still a bug in 5.8.0. We are using replace() in a sorting context to reorder a map of models by their ids.

Calling _setModels with a reordered array using replace() doesn’t trigger a rerender:

_models = observable.map(new Map(), { deep: false });
_setModels(modelsArray) {
    const newModels = modelsArray.map(model => [model.id, model]);
    this._models.replace(newModels);
}

However, this does correctly trigger the rerender:

_models = observable.map(new Map(), { deep: false });
_setModels(modelsArray) {
    const newModels = modelsArray.map(model => [model.id, model]);
    this._models.clear();
    this._models.merge(newModels);
}

Full disclosure, we’re in the process of upgrading from 3.3.1 to 5.8.0 where this previously worked because the implementation was just .clear() and .merge(). Would appear that calling replace() with a reordered list of values, no longer does anything because it’s not removing or adding any keys, which would be fine except the documentation still says:

Replaces the entire contents of this map with the provided values. Short hand for .clear().merge(values)

1reaction
fi3eworkcommented, Dec 12, 2018
this.myMap.replace(myMap) //This is not triggering re-render

you may mean to pass myNewMap, but you passed myMap indeed?

and your second way which works passed into myNewMap

this.myMap.clear()
this.myMap.merge(myNewMap)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscribing to Map Data Structure in JavaScript doesn't work
Do observables not work when adding/removing data from a Map? Here is the observable: test(): Observable<Map<string, Object>> { return ...
Read more >
Map (Java SE 9 & JDK 9 ) - Oracle Help Center
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This...
Read more >
Kivy Documentation - Read the Docs
Kivy empowers you with the freedom to write your code once and have it run as-is on different plat- forms. Follow this guide...
Read more >
Map | Android Developers
However, if the contained keys or values are themselves mutable, this may cause the Map to behave inconsistently or its contents to appear...
Read more >
Coronavirus Disease 2019–COVID-19 - ASM Journals
Coronavirus infection in humans is commonly associated with mild to severe respiratory diseases, with high fever, severe inflammation, cough, and internal organ ...
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