RTK / RTK Query Basic
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.
Redux Toolkitโ-โA Simple Example Workflow
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
export const postsApi = createApi({
  reducerPath: "posts",
  baseQuery: fetchBaseQuery({
    baseUrl: '/api',
    prepareHeaders: (headers) => { // ๋น๋๊ธฐ ๋ก์ง๋ ์์ฑํ  ์ ์์ต๋๋ค.
      ...
      return headers;
    },
  })
  tagTypes: ["Post List"],  // ์บ์ฑ์ ์ฌ์ฉ๋  ํ๊ทธ์ ํ์
  endpoints: (build) => ({
    getPosts: build.query({  // HTTP GET์ ๊ฒฝ์ฐ
      query: () => ({ url: "posts" }),
      provideTags: [{ type: "Post List" }],  // ๋ฐ์์จ ๋ฐ์ดํฐ์ ํ๊ทธ ๋ถ์ฌ
    }),
    postPost: build.mutation({  // POST, PATCH, DELETE ๋ฑ์ ๊ฒฝ์ฐ
      query: (body) => {
        return {
          url: "/posts",
          method: "POST",
          body,
        };
      },
      invalidateTags: [{ type: "Post List" }],  // ํด๋น ํ๊ทธ๋ฅผ ๊ฐ์ง ์บ์ ๋ฐ์ดํฐ ์ ๊ฑฐ(ํ์
 ์ฒดํฌ๋ ์๋์ผ๋ก ๋จ)
    }),
  }),
});invalidatesTags๋ฅผ ์ถ๊ฐํด ๋ฐ์ดํฐ๊ฐ ์ญ์ ๊ฐ ๋๋ฉด ์๋์ผ๋ก ํจ์น๋๋๋ก ํฉ๋๋ค.
deleteTodo: builder.mutation({
      query: (id: number) => ({
        url: `/todo/${id}`,
        method: 'DELETE',
      }),
      invalidatesTags: ['Todos'],
    }),Get all todos
query: () => endpoint
providesTags๋ invalidatesTags๊ฐ ์คํ๋ ๋ ์๋์ผ๋ก ํจ์น๋ ์ฟผ๋ฆฌ
getAllTodos: builder.query<TodoModel[], String>({
      query: () => '/allTodos',
      providesTags: ['Todos'],
    }),Last updated