-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This helper creates a [`shallowRef`](https://vue-composition-api-rfc.netlify.app/api.html#shallowref) (a ref that tracks its own .value mutation but doesn't make its value reactive) that is synced between client & server. Co-authored-by: Sebastian Krüger <2pi_r2@gmx.de>
- Loading branch information
Showing
9 changed files
with
123 additions
and
5 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
--- | ||
|
||
# shallowSsrRef | ||
|
||
This helper creates a [`shallowRef`](https://vue-composition-api-rfc.netlify.app/api.html#shallowref) (a ref that tracks its own .value mutation but doesn't make its value reactive) that is synced between client & server. | ||
|
||
```ts | ||
import { shallowSsrRef, onMounted } from 'nuxt-composition-api' | ||
|
||
const shallow = shallowSsrRef({ v: 'init' }) | ||
if (process.server) shallow.value = { v: 'changed' } | ||
|
||
// On client-side, shallow.value will be { v: changed } | ||
onMounted(() => { | ||
// This and other changes outside of setup won't trigger component updates. | ||
shallow.value.v = 'Hello World' | ||
}) | ||
``` | ||
|
||
::: warning | ||
At the moment, an `shallowSsrRef` is only suitable for one-offs, unless you provide your own unique key. | ||
|
||
This is because server and client `shallowSsrRef` matches up based on line number within your code. | ||
|
||
```ts | ||
function useMyFeature() { | ||
// Only one unique key is generated | ||
const feature = shallowSsrRef('') | ||
return feature | ||
} | ||
|
||
const a = useMyFeature() | ||
const b = useMyFeature() | ||
|
||
b.value = 'changed' | ||
// On client-side, a's value will also be initialised to 'changed' | ||
``` | ||
|
||
If you want to use this pattern, make sure to set a unique key based on each calling of the function. | ||
::: |
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
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
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
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
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
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
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
5ef0f6c
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.
Successfully deployed to following URLs: