TypeScript, React์ Redux-saga๋ก ๊ตฌํํ ๋ ์ ๊ธฐ๋ก ์ฌ์ดํธ
Fastcampus ByteDgree์์ ์งํํ ํ๋ก์ ํธ์ ๋๋ค.
TypeScript
React
Redux
- redux-saga
- ์ฑ ์ถ๊ฐ / ์ฑ ์์ / ์ฑ ์ญ์ / ์ฑ ๋ชฉ๋ก ๋ณด๊ธฐ
- ๋ก๊ทธ์ธ / ๋ก๊ทธ์์
- any ํ์ ์ ์ต๋ํ ์ฌ์ฉํ์ง ์๊ณ , ์ ํํ ํ์ ์ถ๋ก
- form ์ฌ์ฉ ์ uncontrolled component ๋ฐฉ์ ์ฌ์ฉ
- Container components์ Presentational components ๋ถ๋ฆฌํ์ฌ ์์ฑ
- Ducks ํจํด ์ฌ์ฉ
- ๋ก๊ทธ์ธ / ๋ก๊ทธ์์
- ์ฑ ์ถ๊ฐ
- ์ฑ ์์
- ์ฑ ์ญ์
- ์ฑ ๋ชฉ๋ก ๋ณด๊ธฐ
-
Page
- Signin
- Home
- Add
- Detail
- Edit
export const { addBook, editBook, deleteBook, getBooks } = createActions(
{
ADD_BOOK: (book: BookReqType) => ({
book,
}),
EDIT_BOOK: (bookId: number, book: BookReqType) => ({
bookId,
book,
}),
DELETE_BOOK: (bookId: number) => ({ bookId }),
},
'GET_BOOKS',
options,
);
export function* sagas() {
yield takeEvery(`${options.prefix}/GET_BOOKS`, getBooksSaga);
yield takeEvery(`${options.prefix}/ADD_BOOK`, addBookSaga);
yield takeEvery(`${options.prefix}/EDIT_BOOK`, editBookSaga);
yield takeEvery(`${options.prefix}/DELETE_BOOK`, deleteBookSaga);
}
// ์ฑ
๋ชฉ๋ก
function* getBooksSaga() {
try {
yield put(pending());
const token: string = yield select(getTokenFromState);
const books: BookResType[] = yield call(BookService.getBooks, token);
yield put(success(books));
} catch (e) {
yield put(fail(new Error(e?.response?.data?.error || 'UNKNOWN_ERROR')));
}
}
interface addBookSagaAction extends AnyAction {
payload: {
book: BookReqType;
};
}
// ์ฑ
์ถ๊ฐ
function* addBookSaga(action: addBookSagaAction) {
try {
yield put(pending());
const token: string = yield select(getTokenFromState);
const book = action.payload.book;
const bookData = yield call(BookService.addBook, token, book);
const books: BookResType[] = yield select(getBooksFromState);
yield put(success(books.concat(bookData)));
yield put(push('/'));
} catch (e) {
yield put(fail(new Error(e?.response?.data?.error || 'UNKNOWN_ERROR')));
}
}
interface editBookSagaAction extends AnyAction {
payload: {
bookId: number;
book: BookReqType;
};
}
// ์ฑ
์์
function* editBookSaga(action: editBookSagaAction) {
try {
yield put(pending());
const token: string = yield select(getTokenFromState);
const bookId = action.payload.bookId;
const book = action.payload.book;
const bookData = yield call(BookService.editBook, token, bookId, book);
const books: BookResType[] = yield select(getBooksFromState);
yield put(
success(
books.map((book) =>
book.bookId === bookData.bookId ? bookData : book,
),
),
);
yield put(push('/'));
} catch (e) {
yield put(fail(new Error(e?.response?.data?.error || 'UNKNOWN_ERROR')));
}
}
interface delteBookSagaAction extends AnyAction {
payload: {
bookId: number;
};
}
//์ฑ
์ญ์
function* deleteBookSaga(action: delteBookSagaAction) {
try {
yield put(pending());
const token: string = yield select(getTokenFromState);
const bookId = action.payload.bookId;
yield call(BookService.deleteBook, token, bookId);
const books: BookResType[] = yield select(getBooksFromState);
yield put(
success(books.filter((book) => book.bookId !== action.payload.bookId)),
);
} catch (e) {
yield put(fail(new Error(e?.response?.data?.error || 'UNKNOWN_ERROR')));
}
}