Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자료구조
- VUE
- 리덕스
- EBS
- 오라클
- 애그리거트
- 커스텀 로그인
- 트리 회전
- exiting abnormally
- react
- Java Reflextion API
- redux
- 도커빌드
- REDIS
- 컴포넌트 주도
- $emit()
- rabbitmq 에러
- paraller
- 오라클 병렬처리
- Express
- forNmae()
- quert
- 리액트
- ACCESS_REFUSED
- 네임드 뷰
- vue.js
- 자바
- 리덕스 공식문서
- .getClass()
- AWS
Archives
- Today
- Total
개발정리
useReducer 본문
useReducer는 컴포넌트에 reducer를 추가하도록 하는 리액트 훅 입니다.
const [state,dispatch] = useReducer(reducer,initialArg,init?)
Reference
reducer로 state를 관리하기 위해 컴포넌트 상위에 useReducer를 호출하세요
파라미터
-reducer:어떻게 state가 update되는 지를 명시해놓은 reducer 함수입니다.
reducer의 파라미터는 state와 action을 가집니다. 그리고 다음 state를 리턴해야 합니다.
state와 action은 어떠한 타입도 가능합니다.
-initialArg:초기상태로 계산된 값,어떠한 타입도 가능
-(optional)init:초기화함수, 초기 state를 리턴합니다. 만약 정의되어 있지 않다면 초기값은 initialArg로 세팅됩니다.
그렇지 않으면 초기값은 init(initialArg)의 리턴값으로 세팅
Returns
useReducer는 두 값을 가진 배열을 리턴합니다.
1.현재의 state 첫 렌더링 시에 init(initialArg) 또는 initialArg로 세팅
2.dispatch 함수:state를 업데이트하고 리렌더링 됩니다.
예제
import { useReducer } from 'react';
function reducer(state, action){
if(action.type === 'incremented_age'){
return {
age:state.age +1
};
}
throw Error('Unknown action.');
}
function App() {
const [state, dispatch] = useReducer(reducer, {age:42});
return (
<div>
<button onClick = {
()=>{
dispatch({type:'incremented_age'});
}
}>Increment age</button>
<p>Hello! You are {state.age}</p>
</div>
);
}
export default App;
'React.js' 카테고리의 다른 글
스토리북 - 스토리가 뭐야? (0) | 2024.08.22 |
---|---|
스토리북에 대하여... (0) | 2024.08.22 |
리덕스 예제 따라해보기 (0) | 2024.06.30 |
리덕스에 대하여 (0) | 2024.06.29 |
useMemo (0) | 2024.01.13 |