You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- Highlight some of the features your module provide here -->
@@ -30,7 +32,62 @@ Install the module to your Nuxt application with one command:
30
32
npx nuxi module add nuxt-queryref
31
33
```
32
34
33
-
That's it! You can now use `useQueryRef` in your Nuxt app ✨
35
+
That's it! You can now use `queryRef` in your Nuxt app ✨
36
+
37
+
## Usage
38
+
39
+
Use `queryRef()` just like you would use a normal `ref()`
40
+
41
+
```ts
42
+
const variable =queryRef(<key>, <value>)
43
+
```
44
+
45
+
It takes two Parameters:
46
+
47
+
-`<key>`: The key for the URL Query Param
48
+
-`<value>`: The actual value, just like you would use with `ref()`
49
+
50
+
Following types are supported for `<value>`:
51
+
52
+
-`string`
53
+
-`number`
54
+
-`boolean`
55
+
-`Object`
56
+
-`Array (of each of the above)`
57
+
58
+
---
59
+
60
+
### Examples
61
+
62
+
```ts
63
+
const name =queryRef('name', 'Lukas')
64
+
```
65
+
66
+
```ts
67
+
// With generic type (optional, default is based on <value>)
68
+
const name =queryRef<string>('name', 'Lukas')
69
+
```
70
+
71
+
```ts
72
+
// With Object and custom type
73
+
const name =queryRef<{ firstName:string }>('name', { firstName: 'Lukas' })
74
+
```
75
+
76
+
## Use-cases
77
+
78
+
There are multiple scenarios where URL-persisting makes sense:
79
+
80
+
- In general: making URLs shareable or look the same on reload
81
+
- Persisting **_filters or sorting_** for sharing
82
+
- Persisting **_selected tabs or popups_**
83
+
- Persisting **_selected image (-index)_** for e.g. a slider
84
+
85
+
## Under the Hood
86
+
87
+
Some insights:
88
+
89
+
- The value is loaded on page load via **_useRoute()_**, which ensures that the value will already be loaded during **_SSR_** and no flickering will occur
90
+
- If the provided value is an **_array_**, the type will be **_inferred by the first item_** of the value. Therefore mixed-type arrays are currently not supported (or will lead to problems)
0 commit comments