UseOnUnmountDemo
guarahooks/use-on-unmount-demoFreeExample
use-on-unmount's hook in action.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-on-unmount-demo.jsonSource
1'use client';23import React, { useState } from 'react';45import { toast } from 'sonner';67import { Button } from '@/components/ui/button';8import {9 Card,10 CardContent,11 CardDescription,12 CardFooter,13 CardHeader,14 CardTitle,15} from '@/components/ui/card';1617import { useOnUnmount } from '@/hooks/guarahooks/use-on-unmount';1819function ChildComponent() {20 useOnUnmount(() => {21 toast.success('Component unmounted successfully!');22 });2324 return (25 <div className="p-4 rounded-md bg-muted">26 <p>I am the child component. Click to unmount me!</p>27 </div>28 );29}3031export default function UseOnUnmountDemo() {32 const [showChild, setShowChild] = useState(true);3334 return (35 <Card className="relative max-w-md w-full">36 <CardHeader>37 <CardTitle>useOnUnmount</CardTitle>38 <CardDescription>39 This example uses the <code>useOnMount</code> hook to execute a40 function only once when the component is mounted.41 </CardDescription>42 </CardHeader>43 <CardContent>44 <Button onClick={() => setShowChild((prev) => !prev)}>45 {showChild ? 'Unmount child component' : 'Mount child component'}46 </Button>47 <div className="mt-6 min-h-[60px]">48 {showChild && <ChildComponent />}49 </div>50 </CardContent>51 <CardFooter>52 <p className="text-muted-foreground text-sm">53 When the child component is unmounted, a toast will be displayed using54 shadcn/ui.55 </p>56 </CardFooter>57 </Card>58 );59}
What it pulls in
Other registry items
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControllerDemouse-abort-controller-demo | guarahooks | Examples | Free | no deps | |
| UseArrayStateDemouse-array-state-demo | guarahooks | Examples | Free | no deps | |
| UseAxiosDemouse-axios-demo | guarahooks | Examples | Free | no deps | |
| UseBatteryStatusDemouse-battery-status-demo | guarahooks | Examples | Free | no deps | |
| UseBetterAuthDemouse-better-auth-demo | guarahooks | Examples | Free | no deps | |
| UseClickOutsideDemouse-click-outside-demo | guarahooks | Examples | Free | no deps | |
| UseConfirmDemouse-confirm-demo | guarahooks | Examples | Free | no deps | |
| UseCookieDemouse-cookie-demo | guarahooks | Examples | Free | no deps |
