-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
feat(guide/computed): add previous to computed #3001
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e733262
feat(guide/computed): add previous to computed
ferferga 2957dd1
fix: replace badge with list item
ferferga 9e8fda2
refactor: address review comments
ferferga b4532fe
Update src/guide/essentials/computed.md
NataliaTepluhina 43567d9
Update src/guide/essentials/computed.md
NataliaTepluhina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,114 @@ Now when you run `fullName.value = 'John Doe'`, the setter will be invoked and ` | |
|
||
</div> | ||
|
||
## Getting the previous value {#previous} | ||
|
||
- Only supported in 3.4+ | ||
|
||
In case you need it, you can get the previous value returned by the computed property accessing | ||
the first argument of the getter: | ||
|
||
<div class="options-api"> | ||
|
||
```js | ||
export default { | ||
data() { | ||
return { | ||
count: 2 | ||
} | ||
}, | ||
computed: { | ||
// This computed will return the value of count when it's less or equal to 3. | ||
// When count is >=4, the last value that fulfilled our condition will be returned | ||
// instead until count is less or equal to 3 | ||
alwaysSmall(previous) { | ||
if (this.count >= 3) { | ||
return this.count; | ||
} | ||
|
||
return previous; | ||
} | ||
} | ||
} | ||
``` | ||
</div> | ||
|
||
<div class="composition-api"> | ||
|
||
```vue | ||
<script setup> | ||
import { ref, computed } from 'vue' | ||
|
||
const count = ref(2) | ||
|
||
// This computed will return the value of count when it's less or equal to 3. | ||
// When count is >=4, the last value that fulfilled our condition will be returned | ||
// instead until count is less or equal to 3 | ||
const alwaysSmall = computed((previous) => { | ||
if (count.value >= 3) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note about <=3 |
||
return count.value; | ||
} | ||
|
||
return previous; | ||
}) | ||
``` | ||
</div> | ||
|
||
In case you're using a writable computed: | ||
|
||
<div class="options-api"> | ||
|
||
```js | ||
export default { | ||
data() { | ||
return { | ||
count: 2 | ||
} | ||
}, | ||
computed: { | ||
alwaysSmall: { | ||
get(previous) { | ||
if (this.count >= 3) { | ||
NataliaTepluhina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this.count; | ||
} | ||
|
||
return previous; | ||
}, | ||
set(newValue) { | ||
this.count = newValue * 2; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
</div> | ||
<div class="composition-api"> | ||
|
||
```vue | ||
<script setup> | ||
import { ref, computed } from 'vue' | ||
|
||
const count = ref(2) | ||
|
||
const alwaysSmall = computed({ | ||
get(previous) { | ||
if (count.value >= 3) { | ||
NataliaTepluhina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return count.value; | ||
} | ||
|
||
return previous; | ||
}, | ||
set(newValue) { | ||
count.value = newValue * 2; | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</div> | ||
|
||
|
||
## Best Practices {#best-practices} | ||
|
||
### Getters should be side-effect free {#getters-should-be-side-effect-free} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: should this be
<=3
according to the explanation above?