Teaches useReducer for complex state logic in React 19. Use when state updates depend on previous state, multiple related state values, or complex update logic.
Teaches useReducer for managing complex React state with multiple related values and intricate update logic. Use when state depends on previous state or you need to coordinate several state values together.
/plugin marketplace add djankies/claude-configs/plugin install react-19@claude-configsThis skill is limited to using the following tools:
Use useReducer when:
Use useState when:
import { useReducer } from 'react';
const initialState = { count: 0, step: 1 };
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { ...state, count: state.count + state.step };
case 'decrement':
return { ...state, count: state.count - state.step };
case 'setStep':
return { ...state, step: action.payload };
case 'reset':
return initialState;
default:
throw new Error(`Unknown action: ${action.type}`);
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
<p>Count: {state.count}</p>
<p>Step: {state.step}</p>
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
<input
type="number"
value={state.step}
onChange={(e) => dispatch({ type: 'setStep', payload: +e.target.value })}
/>
<button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
</>
);
}
const initialState = {
todos: [],
filter: 'all',
};
function todosReducer(state, action) {
switch (action.type) {
case 'added':
return {
...state,
todos: [...state.todos, { id: Date.now(), text: action.text, done: false }],
};
case 'toggled':
return {
...state,
todos: state.todos.map(t =>
t.id === action.id ? { ...t, done: !t.done } : t
),
};
case 'deleted':
return {
...state,
todos: state.todos.filter(t => t.id !== action.id),
};
case 'filterChanged':
return {
...state,
filter: action.filter,
};
default:
throw new Error(`Unknown action: ${action.type}`);
}
}
For comprehensive useReducer patterns, see: research/react-19-comprehensive.md lines 506-521.
This skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.