-
when observable object in mobx v5 by function, is this
DO NOT USE const { observable, computed, reaction, toJS } = require('../../vendors/mobx')
const store = observable({
list: [
{
name: 'Tom',
date: '2022-04-01',
children: [
{
name: 'TomChild1',
date: '2022-04-02'
}
]
},
{
name: 'Jerry',
date: '2022-04-01',
children: [
{
name: 'JerryChild1',
date: '2022-04-02'
}
]
}
],
get outerList1() {
// Object.is
return this.list
},
get outerList2() {
// deepEqual
// THIS IS QUESTION =======================> Is this correct ?
return computed(() => this.list, { compareStructural: true }).get()
}
})
// Page, onLoad and onShow is Wechat Miniprogram method.
// onShow => when come back this page, trigger onShow hook ( page hide => page show )
Page({
onLoad() {
reaction(
() => toJS(store.outerList1, {
recurseEverything: true
}),
val => {
console.log('outerList1', val)
},
{
fireImmediately: true
}
)
reaction(
() => toJS(store.outerList2, {
recurseEverything: true
}),
val => {
console.log('outerList2 with computed', val)
},
{
fireImmediately: true
}
)
},
onShow() {
console.log('onShow', new Date())
store.list = [
{
name: 'Tom',
date: '2022-04-01',
children: [
{
name: 'TomChild1',
date: '2022-04-02'
}
]
},
{
name: 'Jerry',
date: '2022-04-01',
children: [
{
name: 'JerryChild1',
date: '2022-04-02'
}
]
}
]
}
}) |
Beta Was this translation helpful? Give feedback.
Answered by
urugator
Apr 8, 2022
Replies: 1 comment 1 reply
-
const store = observable({
get outerList2() {
return this.list;
}
}, {
outerList2: computed.struct
}) or const store = observable({
_outerList2: computed(() => store.list, { equals: comparer.structural }),
get outerList2() {
return this._outerList2.get();
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kubk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or