@@ -259,6 +259,112 @@ Now when you run `fullName.value = 'John Doe'`, the setter will be invoked and `
259
259
260
260
</div >
261
261
262
+ ## Getting the previous value <sup class =" vt-badge " data-text =" 3.4+ " /> \*\* {#previous}
263
+
264
+ In case you need it, you can get the previous value returned by the computed property accessing
265
+ the first argument of the getter:
266
+
267
+ <div class =" options-api " >
268
+
269
+ ``` js
270
+ export default {
271
+ data () {
272
+ return {
273
+ count: 2
274
+ }
275
+ },
276
+ computed: {
277
+ // This computed will return the value of count when it's less or equal to 3.
278
+ // When count is >=4, the last value that fulfilled our condition will be returned
279
+ // instead until count is less or equal to 3
280
+ alwaysSmall (previous ) {
281
+ if (this .count >= 3 ) {
282
+ return this .count ;
283
+ }
284
+
285
+ return previous;
286
+ }
287
+ }
288
+ }
289
+ ```
290
+ </div >
291
+
292
+ <div class =" composition-api " >
293
+
294
+ ``` vue
295
+ <script setup>
296
+ import { ref, computed } from 'vue'
297
+
298
+ const count = ref(2)
299
+
300
+ // This computed will return the value of count when it's less or equal to 3.
301
+ // When count is >=4, the last value that fulfilled our condition will be returned
302
+ // instead until count is less or equal to 3
303
+ const alwaysSmall = computed((previous) => {
304
+ if (count.value >= 3) {
305
+ return count.value;
306
+ }
307
+
308
+ return previous;
309
+ })
310
+ ```
311
+ </div >
312
+
313
+ In case you're using a writable computed:
314
+
315
+ <div class =" options-api " >
316
+
317
+ ``` js
318
+ export default {
319
+ data () {
320
+ return {
321
+ count: 2
322
+ }
323
+ },
324
+ computed: {
325
+ alwaysSmall: {
326
+ get (previous ) {
327
+ if (this .count >= 3 ) {
328
+ return this .count ;
329
+ }
330
+
331
+ return previous;
332
+ },
333
+ set (newValue ) {
334
+ this .count = newValue * 2 ;
335
+ }
336
+ }
337
+ }
338
+ }
339
+ ```
340
+
341
+ </div >
342
+ <div class =" composition-api " >
343
+
344
+ ``` vue
345
+ <script setup>
346
+ import { ref, computed } from 'vue'
347
+
348
+ const count = ref(2)
349
+
350
+ const alwaysSmall = computed({
351
+ get(previous) {
352
+ if (count.value >= 3) {
353
+ return count.value;
354
+ }
355
+
356
+ return previous;
357
+ },
358
+ set(newValue) {
359
+ count.value = newValue * 2;
360
+ }
361
+ })
362
+ </script>
363
+ ```
364
+
365
+ </div >
366
+
367
+
262
368
## Best Practices {#best-practices}
263
369
264
370
### Getters should be side-effect free {#getters-should-be-side-effect-free}
0 commit comments