lovable-boilerplate / rules
Cursor rule65 starsChanged 16 months agoMITView on GitHub
Custom hooks patterns for data fetching, forms, and UI state management
In this repository8 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/components.mdcCursor rule
- .cursor/rules/core.mdcCursor rule
- .cursor/rules/design.mdcCursor rule
- .cursor/rules/forms.mdcCursor rule
- .cursor/rules/quality.mdcCursor rule
- .cursor/rules/services.mdcCursor rule
- .cursor/rules/typescript.mdcCursor rule
The file
Files
---
description: Custom hooks patterns for data fetching, forms, and UI state management
globs: ["src/hooks/**/*.{ts,tsx}"]
alwaysApply: false
---
# Custom Hooks Rules
## Hook Organization Patterns
### Data Fetching Hooks
```tsx
// hooks/useUsers.ts
export const useUsers = () => {
return useQuery({
queryKey: ['users'],
queryFn: () => userService.getAllUsers(),
staleTime: 5 * 60 * 1000, // 5 minutes
})
}
export const useUser = (id: string) => {
return useQuery({
queryKey: ['users', id],
queryFn: () => userService.getUser(id),
enabled: !!id,
})
}
```
### Form Hooks
```tsx
// hooks/useUserForm.ts
export const useUserForm = (initialData?: Partial<User>) => {
const form = useForm<UserFormData>({
resolver: zodResolver(userSchema),
defaultValues: initialData || {},
})
const { mutate: saveUser, isPending } = useMutation({
mutationFn: userService.updateUser,
onSuccess: () => toast.success('User saved'),
onError: (error) => toast.error(error.message),
})
return { form, saveUser, isPending }
}
```
### UI State Hooks
```tsx
// hooks/useDisclosure.ts
export const useDisclosure = (initialState = false) => {
const [isOpen, setIsOpen] = useState(initialState)
const open = useCallback(() => setIsOpen(true), [])
const close = useCallback(() => setIsOpen(false), [])
const toggle = useCallback(() => setIsOpen(prev => !prev), [])
return { isOpen, open, close, toggle }
}
```
### Local Storage Hook
```tsx
// hooks/useLocalStorage.ts
export const useLocalStorage = <T>(key: string, initialValue: T) => {
const [storedValue, setStoredValue] = useState<T>(() => {
try {
const item = window.localStorage.getItem(key)
return item ? JSON.parse(item) : initialValue
} catch (error) {
return initialValue
}
})
const setValue = useCallback((value: T | ((val: T) => T)) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value
setStoredValue(valueToStore)
window.localStorage.setItem(key, JSON.stringify(valueToStore))
} catch (error) {
console.error(error)
}
}, [key, storedValue])
return [storedValue, setValue] as const
}
```
## Hook Naming Conventions
- **Data**: `useUsers`, `useUser`, `useProducts`
- **Forms**: `useUserForm`, `useProductForm`
- **UI State**: `useDisclosure`, `useToggle`, `useLocalStorage`
- **Business Logic**: `useAuth`, `useCart`, `useNotifications`
## Hook Extraction Rules
### Extract Logic When:
- Same logic used in 2+ components
- Component exceeds 100 lines due to logic
- Complex state management patterns
- API interaction patterns
### Don't Extract When:
- Logic is component-specific
- Only used once
- Simple useState patterns
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

