출처 - 벨로퍼트
: https://react.vlpt.us/basic/20-useReducer.html
공부를 다시 해야할 거 같음 !
reducer
: 현재 상태와 액션 객체를 파라미터로 받아와서 새로운 상태를 반환해주는 함수
function reducer(state, action) {
// 새로운 상태를 만드는 로직
switch (action.type) {
case 'INCREMENT' :
return state + 1;
case 'DECREMENT' :
return state - 1;
default :
return state;
}
}
state
: 현재 상태
action
: 업데이트를 위한 정보를 가지고 있다. 주로 type
값을 지닌 객체 형태로 사용
const [state, dispatch] = useReducer(reducer, initialState);
state
: 컴포넌트에서 사용할 수 있는 상태
dispatch
(액션을 발생시킨다)
: 액션을 발생시키는 함수 이 함수는 다음과 같이 사용 - dispatch({type : 'INCREMNT'})
useReducer
1) 첫번째 파라미터 : reducer
함수
2) 두번째 파라미터 : 초기상태
Counter.js
import React, { useReducer } from 'react';
function reducer(state, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
function Counter() {
const [number, dispatch] = useReducer(reducer, 0);
const onIncrease = () => {
dispatch({ type: 'INCREMENT' });
};
const onDecrease = () => {
dispatch({ type: 'DECREMENT' });
};
return (
<div>
<h1>{number}</h1>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
);
}
export default Counter;
다음과 같이 변경해보자.
App.js
import React, { useRef, useReducer, useMemo, useCallback } from 'react';
import UserList from './UserList';
import CreateUser from './CreateUser';
function countActiveUsers(users) {
console.log('활성 사용자 수를 세는중...');
return users.filter(user => user.active).length;
}
const initialState = {
inputs: {
username: '',
email: ''
},
users: [
{
id: 1,
username: 'velopert',
email: 'public.velopert@gmail.com',
active: true
},
{
id: 2,
username: 'tester',
email: 'tester@example.com',
active: false
},
{
id: 3,
username: 'liz',
email: 'liz@example.com',
active: false
}
]
};
function reducer(state, action) {
switch (action.type) {
case 'CHANGE_INPUT':
return {
...state,
inputs: {
...state.inputs,
[action.name]: action.value
}
};
case 'CREATE_USER':
return {
inputs: initialState.inputs,
users: state.users.concat(action.user)
};
case 'TOGGLE_USER':
return {
...state,
users: state.users.map(user =>
user.id === action.id ? { ...user, active: !user.active } : user
)
};
case 'REMOVE_USER':
return {
...state,
users: state.users.filter(user => user.id !== action.id)
};
default:
return state;
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState);
const nextId = useRef(4);
const { users } = state;
const { username, email } = state.inputs;
const onChange = useCallback(e => {
const { name, value } = e.target;
dispatch({
type: 'CHANGE_INPUT',
name,
value
});
}, []);
const onCreate = useCallback(() => {
dispatch({
type: 'CREATE_USER',
user: {
id: nextId.current,
username,
email
}
});
nextId.current += 1;
}, [username, email]);
const onToggle = useCallback(id => {
dispatch({
type: 'TOGGLE_USER',
id
});
}, []);
const onRemove = useCallback(id => {
dispatch({
type: 'REMOVE_USER',
id
});
}, []);
const count = useMemo(() => countActiveUsers(users), [users]);
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onToggle={onToggle} onRemove={onRemove} />
<div>활성사용자 수 : {count}</div>
</>
);
}
export default App;
반응형
'TIL > 리액트' 카테고리의 다른 글
리액트 프로젝트에서 타입스크립트 사용하기 (0) | 2022.08.02 |
---|---|
22. Context API를 사용한 전역 값 관리 (0) | 2022.07.29 |
19. React.memo를 사용한 컴포넌트 리렌더링 방지 (0) | 2022.07.26 |
18. useCallback을 사용하여 함수 재사용하기 (0) | 2022.07.23 |
17. useMemo를 사용하여 연산한 값 재사용하기 (0) | 2022.07.22 |
댓글