RTK / RTK Query Basic
Last updated
Last updated
createAction
๋ ๊ฐ์ง ์ธ์๋ฅผ ์ทจํฉ๋๋ค. ์ฒซ ๋ฒ์งธ๋ ์์
์ ํ์ด๋ฉฐ ํ์์
๋๋ค. ๋ ๋ฒ์งธ ์ธ์๋ ์์ prepare
๋ก, ๊ฒฐ๊ณผ ์์
์์ฑ์๋ก๋ถํฐ ์ธ์๋ฅผ ์๋ฝํ๊ณ ์ด๋ฌํ ์ธ์๋ฅผ ์ถ๊ฐ ๋ฐ์ดํฐ๋ก ์์
๊ฐ์ฒด์ ์ฒจ๋ถํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ค๋น ๊ธฐ๋ฅ์ ์ ํ ์ฌํญ์
๋๋ค .
createAction
takes two arguments. The first one is the action type and it's required. The second argument is a so-called prepare function which wecan use to accept arguments from the resulting action creator and attach these arguments as additional data to the action object. The prepare function is optional.
extraReducers
allows createSlice
to respond to other action types besides the types it has generated.
extraReducers
๋ฅผ ์ฌ์ฉํ๋ฉด createSlice
๊ฐ ์์ฑํ ์์
์ ํ ์ด์ธ์ ๋ค๋ฅธ ์์
์ ํ์ ์๋ตํ ์ ์์ต๋๋ค.
As case reducers specified with extraReducers
are meant to reference "external" actions, they will not have actions generated in slice.actions
.
As with reducers
, these case reducers will also be passed to createReducer
and may "mutate" their state safely.
The recommended way of using extraReducers
is to use a callback that receives a ActionReducerMapBuilder
instance.
unwrap()
we can await addNewPost().unwrap()
to handle any potential errors with a standard try/catch
block.
RTK Query
invalidatesTags
๋ฅผ ์ถ๊ฐํด ๋ฐ์ดํฐ๊ฐ ์ญ์ ๊ฐ ๋๋ฉด ์๋์ผ๋ก ํจ์น๋๋๋ก ํฉ๋๋ค.
Get all todos
query: () => endpoint
providesTags๋ invalidatesTags๊ฐ ์คํ๋ ๋ ์๋์ผ๋ก ํจ์น๋ ์ฟผ๋ฆฌ