---
name: react-hook-optimizer
description: "Extracts logic from a React component into a custom hook and trims needless re-renders, giving you the hook file and a cleaner component. Use when a React component mixes data fetching or state logic with markup and needs that logic moved into a reusable custom hook. Do not use for migrating a class component or choosing a state management library. Related topics: react, javascript, coding."
---

# React Hook Optimizer

Extracts logic from a React component into a custom hook and trims needless re-renders, giving you the hook file and a cleaner component.

## When to use

Use this skill when a React component mixes data fetching or state logic with markup and needs that logic moved into a reusable custom hook. Do not use it for migrating a class component or choosing a state management library.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Inventory the component's state, effects and handlers and mark what belongs to the extracted logic.
3. Write the custom hook so it owns that logic and returns only what the component consumes.
4. Check dependency arrays and effect cleanup so behaviour is unchanged.
5. Apply useCallback and useMemo only where a stable reference matters.
6. Return the hook file and the updated component with a short list of changes.

## Inputs

- **Component code** (longer free text)
- **Logic to extract** (longer free text)
- **Language** (choose one: JavaScript, TypeScript)
- **Use useCallback and useMemo to prevent re-renders** (choose one: Yes, No)
- **Add comments explaining the hook** (choose one: Yes, No)

## Output

Two code blocks, the hook file and the updated component, followed by a brief change list and any remaining render concerns.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### React Hook Optimizer

Refactor a React component by moving logic into a custom hook.

- Component code: {{Component code}}
- Logic to extract: {{Logic to extract}}
- Language: {{Language}}
- Use useCallback and useMemo to prevent re-renders: {{Use useCallback and useMemo to prevent re-renders}}
- Add comments explaining the hook: {{Add comments explaining the hook}}

First list the state, effects and handlers in the component and mark which belong in the extracted logic. Create a custom hook named with the use prefix that owns that state and returns only what the component needs. Keep dependency arrays correct and clean up any subscriptions or requests in the effect. If memoisation is on, apply it only where a value or callback is passed to a child or used as a dependency, since blanket memoisation adds cost for no gain. Preserve behaviour exactly.

Return two files: the hook and the updated component, with TypeScript types when selected. After the code, list what changed and note any render issue that remains, such as an unstable prop from the parent.

## Pitfalls

- Wrapping every function in useCallback, which adds overhead without preventing renders.
- Leaving out values from dependency arrays and creating stale closures.
- Returning the hook's whole internal state when the component needs only two values.
