-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
What's difference between @computed decorator and getter function #161
Comments
From the docs
...
So if you're going to observe it, make it a computed property. |
What I think is firstName and lastName is being observed in both computed and getter function. At first I thought the use case is for optimize as the docs said that the computed properties will not invoked when there input parameters didn't modify. class Test {
@observable firstName = 'first'
@observable lastName = 'last'
@computed
get fullName () {
return `${this.firstName} ${this.lastName}`
}
getFullName () {
return `${this.firstName} ${this.lastName}`
}
}
const test = new Test()
autorun(() => {
console.log('computed:', test.fullName)
})
autorun(() => {
console.log('getter:', test.getFullName())
})
test.firstName = 'salmon'
test.lastName = 'sushi'
test.firstName = 'salmon' // <---- again
// computed: first last
// getter: first last
// computed: salmon last
// getter: salmon last
// computed: salmon sushi
// getter: salmon sushi But after I tried, the getter function didn't print duplicate fullName when I assign the firstName "salmon" again. Thanks. |
Both solutions will exhibit the same behavior, but introducing If If, in contrast, Also, if you read So, when in doubt, use it (if your function is pure in terms of depending only on observable values, which it should be) |
Got it! thank you very much :) |
@mweststrate so it seems there is not much difference between using a computed decorator and a simple getter? (besides optimization?) |
Correct. But the optimization can be huge ;-). And computed protects
against common errors like modifying state and such, and clearly signal to
the reader this is derived data.
Op wo 26 jul. 2017 om 00:07 schreef Giorgi Moniava <[email protected]
…:
@mweststrate <https://github.com/mweststrate> so it seems there is not
much difference between using a computed decorator and a simple getter?
(besides optimization?)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#161 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhF4dSxYS_IZCtgY_6DjixFY-kJTnks5sRmcsgaJpZM4Hw0Ta>
.
|
Who can be notified? I tried hard, and every time I get not a cached value. So I confused :) |
When would I get a cached result instead of getting the function recomputed? I tried the following example and it recomputes. const {observable, computed} = mobx;
class Test {
@observable firstName = 'first'
@observable lastName = 'last'
@computed
get fullName () {
console.log('recompute fullname');
return `${this.firstName} ${this.lastName}`
}
}
let test = new Test()
console.log(test.fullName);
//recompute fullname
//first last
console.log(test.fullName);
//recompute fullname
//first last |
@windwalker I was confused by that too, but it seems values are cached when used inside a Something like https://jsbin.com/rujuxe/edit?js,console,output you can see that the computed values are only called once even when we show then twice. Also the value will be reevaluated if called directly. (check the click handler of the button) @mweststrate are there any plans to also return a cached value from the normal usages outside of |
No, that would kill garbage collection; every computed would stay alive and
keep recomputing as long as it observes anything that is alive.
Basically you shouldn't care about whether it is cached or not: the result
should be the same. If it matters, that could be a smell
…On vr 5 jan. 2018 05:48 Roy Riojas ***@***.***> wrote:
@windwalker <https://github.com/windwalker> I was confused by that too,
but it seems values are cached when used inside a reaction, autorun or in
the render of an observable React component.
Something like https://jsbin.com/rujuxe/edit?js,console,output you can
see that the computed values are only called once even when we show then
twice.
Also the value will be reevaluated if called directly. (check the click
handler of the button)
@mweststrate <https://github.com/mweststrate> are there any plans to also
return a cached value from the normal usages outside of autorun, reaction
and render?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#161 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhPTK71eTzF8KRld9U9o9qsDW8620ks5tHamNgaJpZM4Hw0Ta>
.
|
I'm not sure when should i use the @computed decorator (without asStructure option) in class instead of the getter function for example
Thanks
The text was updated successfully, but these errors were encountered: