|
| 1 | +# Extensions |
| 2 | + |
| 3 | +Extensions allow you to assign any additional properties to the store, while keeping the store definition self-contained and reusable. |
| 4 | + |
| 5 | +Under the hood, `actions`, `computed` and `effects` all wrap around the `extend` method. |
| 6 | + |
| 7 | +However, you can also use `extend` directly to add any custom properties to the store, which don't direclty fit into the state, actions or computed properties. |
| 8 | + |
| 9 | +## Basic usage example |
| 10 | + |
| 11 | +```tsx |
| 12 | +import { store } from '@davstack/store'; |
| 13 | + |
| 14 | +const userStore = store() |
| 15 | + .state({ |
| 16 | + name: 'John', |
| 17 | + age: 25, |
| 18 | + }) |
| 19 | + .extend((store) => ({ |
| 20 | + isAdmin: false, |
| 21 | + })); |
| 22 | + |
| 23 | +// accessing the extension |
| 24 | + |
| 25 | +const isAdmin = userStore.isAdmin.get(); |
| 26 | +``` |
| 27 | + |
| 28 | +## Example usage with hooks |
| 29 | + |
| 30 | +```tsx |
| 31 | + |
| 32 | +const altStore = store({ |
| 33 | + searchTerm: '', |
| 34 | +}).extend((store) => ({ |
| 35 | + useFilteredBooks: () => { |
| 36 | + const searchTerm = store.searchTerm.use(); |
| 37 | + // use react query or any other data fetching library here |
| 38 | + |
| 39 | + }, |
| 40 | + |
| 41 | +``` |
| 42 | +
|
| 43 | +## Example usage with components |
| 44 | +
|
| 45 | +```tsx |
| 46 | +import { store } from '@davstack/store'; |
| 47 | + |
| 48 | +export const notificationsStore = store({ |
| 49 | + subscription: null as PushSubscription | null, |
| 50 | + registration: null as ServiceWorkerRegistration | null, |
| 51 | +}). |
| 52 | +.computed((store) => ({ |
| 53 | + isSubscribed: () => Boolean(store.subscription), |
| 54 | +})) |
| 55 | +.extend((store) => { |
| 56 | + async function init() { |
| 57 | + const registration = await navigator.serviceWorker.ready; |
| 58 | + |
| 59 | + try { |
| 60 | + checkPushNotificationIsSupported(); |
| 61 | + |
| 62 | + const subscription = await registration.pushManager.getSubscription(); |
| 63 | + |
| 64 | + if (!subscription) { |
| 65 | + console.log("No subscription found"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + await apiUtils!.notification.checkSubscription.fetch({ |
| 71 | + endpoint: subscription.endpoint, |
| 72 | + }); |
| 73 | + |
| 74 | + store.subscription.set(subscription); |
| 75 | + } catch (error) { |
| 76 | + console.error("Error initializing subscription:", error); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + /** |
| 82 | + * Initializes the store, should only be place once in root layout |
| 83 | + */ |
| 84 | + Init() { |
| 85 | + useEffect(() => { |
| 86 | + init(); |
| 87 | + }, []); |
| 88 | + |
| 89 | + return null; |
| 90 | + } |
| 91 | + }; |
| 92 | +}); |
| 93 | + |
| 94 | +// app/layout.tsx |
| 95 | +import { notificationsStore } from './notificationsStore'; |
| 96 | + |
| 97 | +export default function Layout({ children }:{children: React.ReactNode}) { |
| 98 | + return ( |
| 99 | + <div> |
| 100 | + {children} |
| 101 | + <notificationsStore.Init> |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +``` |
0 commit comments