Build reusable custom hooks in React to share logic between components. Learn extraction patterns, naming conventions, and best practices for stateful logic reuse.
Build reusable custom hooks in React to share stateful logic between components without duplicating code.
This skill helps you create custom React hooks that extract and share component logic. You'll learn how to identify reusable patterns, properly name hooks, share stateful logic (not state itself), and follow React conventions.
When a user asks you to create or refactor code using custom hooks, follow these steps:
Look for patterns where multiple components use similar state and effects. Common candidates:
**Naming Convention:**
**Structure:**
```javascript
function useCustomHook() {
const [state, setState] = useState(initialValue);
useEffect(() => {
// Setup subscription or side effect
return () => {
// Cleanup
};
}, [dependencies]);
return state; // or object with multiple values
}
```
Extract the complete logic including:
Replace duplicated code with the custom hook call:
```javascript
function MyComponent() {
const value = useCustomHook();
// Use value in component
}
```
**DO:**
**DON'T:**
**Network Status:**
```javascript
function useOnlineStatus() {
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
function handleOnline() { setIsOnline(true); }
function handleOffline() { setIsOnline(false); }
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
return isOnline;
}
```
**Form Input:**
```javascript
function useFormInput(initialValue) {
const [value, setValue] = useState(initialValue);
function handleChange(e) {
setValue(e.target.value);
}
return {
value,
onChange: handleChange
};
}
```
**Data Fetching:**
```javascript
function useData(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
let ignore = false;
fetch(url)
.then(res => res.json())
.then(data => {
if (!ignore) {
setData(data);
setLoading(false);
}
})
.catch(err => {
if (!ignore) {
setError(err);
setLoading(false);
}
});
return () => { ignore = true; };
}, [url]);
return { data, loading, error };
}
```
Remember: Each component that calls a custom hook gets its own isolated state. If two components call `useOnlineStatus()`, they each have separate `isOnline` state variables that happen to have the same value because they're synchronized with the same external source (network status).
Extract a custom hook when:
Don't extract if:
**User Request:** "I have two components that both check if the user is online. Can you help me avoid the duplication?"
**Your Response:**
1. Show the custom hook extraction (`useOnlineStatus.js`)
2. Demonstrate usage in both components
3. Explain that each component gets its own state, synchronized with the same external source
4. Point out the benefits: less duplication, clearer component code, reusable logic
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/react-custom-hooks/raw