> For the complete documentation index, see [llms.txt](https://k-developer.gitbook.io/dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://k-developer.gitbook.io/dev/react/react-hook/usereducer.md).

# useReducer

> 컴포넌트의 상태값을 리덕스의 리듀서 처럼 관리하기

```javascript

const INITIAL_STATE = { name: '' }; // 초기 상태값

const reducer = (state, action) => {
  switch (action.type) {
    case 'setName':
      return {
        ...state,
        name: action.name,
      };
  }
};

function App() {
  const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
  const onChange = (e) => {
    dispatch({ type: 'setName', name: e.target.value });
  };

  return (
    <div className="App">
      <input type="text" value={state.name} onChange={onChange} />
    </div>
  );
}

```

> 데이터 흐름

onChange -> dispatch -> action -> reducer (setName) -> update state -> 랜더링

리덕스와 같은 방식으로 작동한다. state를 직접 업데이트 하지 않고 dispatch action 을 통해 관리한다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://k-developer.gitbook.io/dev/react/react-hook/usereducer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
