← スキル一覧に戻る

react-hooks
by fusengine
Redefining development through cognitive automation and collaborative agent systems.
⭐ 0🍴 0📅 2026年1月25日
SKILL.md
name: react-hooks description: React hooks patterns - useState, useEffect, useCallback, useMemo, useRef, custom hooks. Use when implementing React hooks or creating custom hooks. user-invocable: false
React Hooks Guide
Basic Hooks
useState
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
// Functional update for derived state
const increment = () => setCount(prev => prev + 1)
return <button onClick={increment}>{count}</button>
}
useEffect
import { useState, useEffect } from 'react'
function OnlineStatus() {
const [isOnline, setIsOnline] = useState(true)
useEffect(() => {
const handleOnline = () => setIsOnline(true)
const handleOffline = () => setIsOnline(false)
window.addEventListener('online', handleOnline)
window.addEventListener('offline', handleOffline)
// Cleanup
return () => {
window.removeEventListener('online', handleOnline)
window.removeEventListener('offline', handleOffline)
}
}, []) // Empty deps = mount only
return <span>{isOnline ? '✅ Online' : '❌ Offline'}</span>
}
useContext
import { useContext, createContext } from 'react'
const UserContext = createContext<User | null>(null)
function useUser() {
const context = useContext(UserContext)
if (!context) throw new Error('useUser must be within UserProvider')
return context
}
Performance Hooks
useMemo
Memoize expensive computations.
import { useMemo } from 'react'
function ExpensiveList({ items, filter }: Props) {
const filteredItems = useMemo(
() => items.filter(item => item.name.includes(filter)),
[items, filter]
)
return <ul>{filteredItems.map(item => <li key={item.id}>{item.name}</li>)}</ul>
}
useCallback
Memoize functions for stable references.
import { useCallback } from 'react'
function TodoList({ todos, onToggle }: Props) {
const handleToggle = useCallback(
(id: string) => onToggle(id),
[onToggle]
)
return todos.map(todo => (
<TodoItem key={todo.id} todo={todo} onToggle={handleToggle} />
))
}
useRef
Persist values without re-renders.
import { useRef, useEffect } from 'react'
function TextInput() {
const inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
inputRef.current?.focus()
}, [])
return <input ref={inputRef} />
}
Custom Hooks Patterns
useFormInput
import { useState, ChangeEvent } from 'react'
/**
* Manage form input state.
*/
export function useFormInput(initialValue: string) {
const [value, setValue] = useState(initialValue)
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value)
}
return { value, onChange: handleChange }
}
// Usage
function Form() {
const firstName = useFormInput('')
const lastName = useFormInput('')
return (
<form>
<input {...firstName} placeholder="First name" />
<input {...lastName} placeholder="Last name" />
</form>
)
}
useLocalStorage
import { useState, useEffect } from 'react'
/**
* Sync state with localStorage.
*/
export function useLocalStorage<T>(key: string, initialValue: T) {
const [value, setValue] = useState<T>(() => {
const stored = localStorage.getItem(key)
return stored ? JSON.parse(stored) : initialValue
})
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value))
}, [key, value])
return [value, setValue] as const
}
useDebounce
import { useState, useEffect } from 'react'
/**
* Debounce a value.
*/
export function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay)
return () => clearTimeout(timer)
}, [value, delay])
return debouncedValue
}
useFetch
import { useState, useEffect } from 'react'
interface UseFetchResult<T> {
data: T | null
loading: boolean
error: Error | null
}
/**
* Fetch data from URL.
*/
export function useFetch<T>(url: string): UseFetchResult<T> {
const [data, setData] = useState<T | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
let cancelled = false
fetch(url)
.then(res => res.json())
.then(json => { if (!cancelled) setData(json) })
.catch(err => { if (!cancelled) setError(err) })
.finally(() => { if (!cancelled) setLoading(false) })
return () => { cancelled = true }
}, [url])
return { data, loading, error }
}
Rules of Hooks
- Only call at top level - Not in loops, conditions, or nested functions
- Only call in React functions - Components or custom hooks
- Name custom hooks with
use-useUser,useFetch, etc. - Keep hooks focused - One concern per hook
- Document with JSDoc - Explain purpose and return value
スコア
総合スコア
60/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です