Skip to content

Commit b47a194

Browse files
committed
computed best practices
1 parent 253a2ae commit b47a194

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/guide/essentials/computed.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ A computed property automatically tracks its reactive dependencies. Vue is aware
126126

127127
</div>
128128

129-
It is important to remember that **computed getter functions should be free of side effects**. For example, don't make async requests or mutate the DOM inside a computed getter! Think of a computed property has declaratively describing how to derive a value based on other values - its only resposnibility should be computing and returning that value. Later in the guide we will discuss how we can perform side effects in reaction to state changes with [watchers](./watchers).
130-
131129
## Computed Caching vs Methods
132130

133131
You may have noticed we can achieve the same result by invoking a method in the expression:
@@ -280,3 +278,13 @@ const fullName = computed({
280278
Now when you run `fullName.value = 'John Doe'`, the setter will be invoked and `firstName` and `lastName` will be updated accordingly.
281279

282280
</div>
281+
282+
## Best Practices
283+
284+
### Getters should be side-effect free
285+
286+
It is important to remember that computed getter functions should only perform pure computation and be free of side effects. For example, don't make async requests or mutate the DOM inside a computed getter! Think of a computed property has declaratively describing how to derive a value based on other values - its only resposnibility should be computing and returning that value. Later in the guide we will discuss how we can perform side effects in reaction to state changes with [watchers](./watchers).
287+
288+
### Avoid mutating computed value
289+
290+
The returned value from a computed property is derived state. Think of it as a temporary snapshot - every time the source state changes, a new snapshot is created. It does not make sense to mutate a snapshot, so a computed return value should be treated as read-only and never be mutated - instead, update the source state it depends on to trigger new computations.

src/guide/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Different developers have different learning styles. Feel free to pick a learnin
213213
</a>
214214
<a class="vt-box" href="/guide/quick-start.html">
215215
<p class="next-steps-link">Continue the Guide</p>
216-
<p class="next-steps-caption">An in-depth guide that walks through the core concepts with small details.</p>
216+
<p class="next-steps-caption">An in-depth guide that walks through the core concepts in full detail.</p>
217217
</a>
218218
<a class="vt-box" href="/examples/">
219219
<p class="next-steps-link">Check out the Examples</p>

src/guide/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ If you skipped the [Introduction](/guide/introduction), we strongly recommend re
141141
</a>
142142
<a class="vt-box" href="/guide/essentials/application.html">
143143
<p class="next-steps-link">Continue the Guide</p>
144-
<p class="next-steps-caption">An in-depth guide that walks through the core concepts with small details.</p>
144+
<p class="next-steps-caption">An in-depth guide that walks through the core concepts in full detail.</p>
145145
</a>
146146
<a class="vt-box" href="/examples/">
147147
<p class="next-steps-link">Check out the Examples</p>

0 commit comments

Comments
 (0)