-
I have following js code: import { observable, action } from 'mobx'; // mobx version "5.8.0"
class MyClass {
@observable
public data = {
id: "xxx",
name: "yyy",
myArray: [] // <----- this will stores some complex objects, not simple number/string elements
};
@action
public updateMyElems = (arr) => {
this.data.myArray = arr;
}
}
const myClass = new MyClass();
const largeArr = [ ... ]; // 1000,000 elements in this array
myClass.updateMyElems(largeArr) <--------- extremely slow the line |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I tried with a workround since I believe update the oberverable array is complicated / expensive operation: @action
public updateMyElems = (arr) => {
const pureJsObj = toJS(this.data);
pureJsObj.myArr = arr;
this.data = pureJsObj; // bypass the internal complex logic for array
} somehow I wonder is there a more elegance way instead this. |
Beta Was this translation helpful? Give feedback.
-
Quick check: Are you using 'action' on the event handler that triggers the
changes? If not you really should
…On Fri, Dec 6, 2024, 18:30 yanzou ***@***.***> wrote:
@mweststrate <https://github.com/mweststrate>
The code above was abstracted/simplified logic from a complex project, but
from a high level, the thing I trying to do to is:
- MyClass.data (including data.myArray) is observing by tons of
components.
- When the page is load 1st time, that array can be empty or contain
some initail values
- There will be a GUI action triggered by user (button click or
whatever make sense), a logic will generate 1000k new elements and replaces
whatever in the array.
I am gussing if use @observable.shallow, I possibly need to separate the
array from data.myArray? some like
class MyClass {
@observable
public data = {
id: "xxx",
name: "yyy"
};
@observable.shallow
public myArray: any[] = [];
...
I am not sure if I can do this as I mentioned above it is a complicated
project if I do so need a refectory on the project. But I think this is
definitely a good direction, thanks a lot!
—
Reply to this email directly, view it on GitHub
<#3965 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBECAB2PPCCTUTAKNBL2EHNKTAVCNFSM6AAAAABTDWIPGKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNBYG42TGNQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I finially relized it possibly not just the oberserable array itself but with other computed properties watching the data:
class MyClass {
...
@action
public updateMyElems = (arr) => {
this.data.myArray = arr;
}
// some computed flag depends on `myArray`
@computed
public get isOneOfElementOk() {
return this.data.myArray.find(ele => ele.XXX.yyy.zzz === 'something' )
}
....
} According to mobx doc, the |
Beta Was this translation helpful? Give feedback.
-
Just a FYI |
Beta Was this translation helpful? Give feedback.
I finially relized it possibly not just the oberserable array itself but with other computed properties watching the data:
MyClass
is much much complicated, it has some other computed props oberserving the array, something like:According to mobx doc, the
action
internally al…