{"id":395558,"date":"2024-06-29T12:17:27","date_gmt":"2024-06-29T12:17:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=395558"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T12:17:27","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=395558","title":{"rendered":"<span>RTK Query \u0438 redux. \u041c\u0438\u043d\u0438\u043c\u0443\u043c \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 api<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435 \u043c\u044b \u043d\u0435 \u043b\u044e\u0431\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u0445\u043e\u0442\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430) \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API &#8212; <a href=\"https:\/\/redux-toolkit.js.org\/rtk-query\/overview\" rel=\"noopener noreferrer nofollow\">RTK Query<\/a>. Redux \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043d\u0435\u0439, \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u043e\u0441\u0442\u0438 \u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, RTK query \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 Redux.<\/p>\n<h4>\u0417\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430<\/h4>\n<p>\u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043a API \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Redux \u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 RTK query. \u0421\u043d\u0430\u0447\u0430\u043b\u0430, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043a\u043e\u0434, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a API \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Redux \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u0442\u043e\u0432:<\/p>\n<pre><code> \/\/ actions.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043a\u0448\u0435\u043d\u043e\u0432 export const fetchPostsRequest = () => ({   type: 'FETCH_POSTS_REQUEST', });  export const fetchPostsSuccess = (posts) => ({   type: 'FETCH_POSTS_SUCCESS',   payload: posts, });  export const fetchPostsFailure = (error) => ({   type: 'FETCH_POSTS_FAILURE',   payload: error, });  \/\/ reducer.js \/\/ \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 Redux-\u0441\u0442\u0435\u0439\u0442\u0430 const initialState = {   posts: [],   isLoading: false,   error: null, };  \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0434\u044e\u0441\u0435\u0440\u043e\u0432 export default function postsReducer(state = initialState, action) {   switch (action.type) {     case 'FETCH_POSTS_REQUEST':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 true, \u043f\u043e\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f       return { ...state, isLoading: true };     case 'FETCH_POSTS_SUCCESS':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 false \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u0442\u0435\u0439\u0442       return { ...state, isLoading: false, posts: action.payload };     case 'FETCH_POSTS_FAILURE':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 false \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0432 \u0441\u0442\u0435\u0439\u0442       return { ...state, isLoading: false, error: action.payload };     default:       return state;   } }  \/\/ thunk.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 import { fetchPostsRequest, fetchPostsSuccess, fetchPostsFailure } from '.\/actions';  export const fetchPosts = () => async (dispatch) => {   dispatch(fetchPostsRequest());   try {     const response = await fetch('\/api\/posts');     const data = await response.json();     dispatch(fetchPostsSuccess(data));   } catch (error) {     dispatch(fetchPostsFailure(error.message));   } };  \/\/selectors.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430 export const postsSelector = state => state.posts  \/\/ component.js import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { fetchPosts } from '.\/thunk'; import { postsSelector } from '.\/selectors.js'  export default function Posts() {   const dispatch = useDispatch();   const posts = useSelector(postsSelector);   const isLoading = useSelector((state) => state.isLoading);   const error = useSelector((state) => state.error);    useEffect(() => {     \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430     dispatch(fetchPosts());   }, [dispatch]);    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (error) {     return &lt;div>Error: {error}&lt;\/div>;   }    return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>{post.title}&lt;\/li>       ))}     &lt;\/ul>   ); }  <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 RTK query:<\/p>\n<pre><code>\/\/ service.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',     }),   }), });  export const { useGetPostsQuery } = postsApi;  \/\/ component.js import React, { useEffect } from 'react'; import { useGetPostsQuery } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery();    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (isError) {     return &lt;div>Error: {error.data}&lt;\/div>;   }    return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>{post.title}&lt;\/li>       ))}     &lt;\/ul>   ); }  <\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0431\u0435\u0437 RTK Query \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043a\u0448\u0435\u043d\u043e\u0432, \u0440\u0435\u0434\u044e\u0441\u0435\u0440\u043e\u0432, thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430 \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 Redux \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a API \u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e <code>useGetPostsQuery<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u0442\u043e\u0432.<\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0438 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. RTK Query \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h4>\u041a\u044d\u0448\u0435\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u044d\u0433\u0438<\/h4>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432 \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 RTK Query \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u043f\u043e\u0441\u0442\u0430:<\/p>\n<pre><code>\/\/ service.js import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   tagTypes: ['Posts',],   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',       providesTags: ['Post'],     }),     addPost: builder.mutation({       query: (body) => ({         url: '\/posts',         method: 'POST',         body,       }), \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0430        invalidatesTags: ['Posts'],     }),   }), });  export const { useGetPostsQuery, useAddPostMutation } = postsApi;  \/\/ component.js import React, { useEffect, useState } from 'react'; import { useGetPostsQuery, useAddPostMutation } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery();   const [title, setTitle] = useState('');   const [body, setBody] = useState('');   const [addPost, { isError: addError }] = useAddPostMutation();    const handleAddPost = async () => {     await addPost({ title, body });     setTitle('');     setBody('');   };    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (isError || addError) {     return &lt;div>Error: {error?.data || addError?.data}&lt;\/div>;   }    return (     &lt;div>       &lt;form onSubmit={handleAddPost}>         &lt;input type=\"text\" placeholder=\"Title\" value={title} onChange={(e) => setTitle(e.target.value)} \/>         &lt;textarea placeholder=\"Body\" value={body} onChange={(e) => setBody(e.target.value)} \/>         &lt;button type=\"submit\">Add Post&lt;\/button>       &lt;\/form>       &lt;ul>         {posts.map((post) => (           &lt;li key={post.id}>{post.title}&lt;\/li>         ))}       &lt;\/ul>     &lt;\/div>   ); }  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a. \u0417\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0442\u044d\u0433\u0438. \u0422\u0435\u0433\u0438 \u0432 RTK query \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u044d\u0448\u0435\u043c. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>providesTags<\/code> \u0438 <code>invalidatesTags<\/code> \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 <code>query<\/code> \u0438 <code>mutation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c RTK query, \u043a\u0430\u043a\u0438\u0435 \u0442\u0435\u0433\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u043c\u0438 \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 \u044d\u0442\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438.<\/p>\n<p>\u0422\u0435\u0433\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>providesTags<\/code>, \u0431\u0443\u0434\u0443\u0442 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043f\u043e\u0441\u0442\u043e\u0432, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>getPosts<\/code>. \u0422\u0435\u0433\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>invalidatesTags<\/code>, \u0431\u0443\u0434\u0443\u0442 \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>addPost<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0441\u0442\u043e\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0430\u043c\u0438\u043c \u0432 \u043d\u0443\u0436\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a:<\/p>\n<pre><code>&lt;Button onClick={()=>postsApi.invalidateQueries(\"Post\")}> Update &lt;\/Button> <\/code><\/pre>\n<h4>transformResponse<\/h4>\n<p><code><strong>transformResponse<\/strong><\/code> &#8212; \u044d\u0442\u043e \u043e\u043f\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u0438\u0437 \u0441\u0435\u0442\u0438, \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u043e\u043c. \u041e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438\u0442.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0446\u0438\u044e <code><strong>transformResponse<\/strong><\/code> \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 <code><strong>date<\/strong><\/code> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043f\u0438\u0441\u043a\u0430:<\/p>\n<pre><code>\/\/ service.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',       transformResponse: (response) => {         \/\/ \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0443         const posts = response.map((post) => ({           ...post,           date: new Date(),         }));         return posts;       },     }),   }), });  export const { useGetPostsQuery } = postsApi;  \/\/ component.js import React, { useEffect } from 'react'; import { useGetPostsQuery } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery(); ...   return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>           {post.title} - {post.date.toLocaleString()}         &lt;\/li>       ))}     &lt;\/ul>   ); } <\/code><\/pre>\n<h4>queryFn<\/h4>\n<p><code><strong>queryFn<\/strong><\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u043b\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440: \u043e\u0434\u0438\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u043e\u0432, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u044d\u0442\u0438\u0445 \u043f\u043e\u0441\u0442\u043e\u0432. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code><strong>queryFn<\/strong><\/code> \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>\/\/ service.js import { createApi } from '@reduxjs\/toolkit\/query';  export const blogApi = createApi({   reducerPath: 'blogApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getBlogData: builder.query({       queryFn: async () => {         const [posts, authors] = await Promise.all([           fetch('\/api\/posts').then((res) => res.json()),           fetch('\/api\/authors').then((res) => res.json()),         ]);         return { posts, authors };       },       transformResponse: (response) => {         const { posts, authors } = response;         const authorsById = {};         authors.forEach((author) => {           authorsById[author.id] = author;         });         const postsWithAuthors = posts.map((post) => ({           ...post,           author: authorsById[post.authorId],         }));         return postsWithAuthors;       },     }),   }), });  export const { useGetBlogDataQuery } = blogApi;  \/\/ component.js import React from 'react'; import { useGetBlogDataQuery } from '.\/service';  export default function Blog() {   const { data: blogData, isLoading, isError, error } = useGetBlogDataQuery(); ...   return (     &lt;ul>       {blogData.map((post) => (         &lt;li key={post.id}>           &lt;h3>{post.title}&lt;\/h3>           &lt;p>{post.content}&lt;\/p>           &lt;p>             Written by {post.author.name} ({post.author.email})           &lt;\/p>         &lt;\/li>       ))}     &lt;\/ul>   ); } <\/code><\/pre>\n<p>\u041d\u0443 \u0438\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u043e\u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0438 \u043f\u043e\u0441\u0442\u043e\u0432 \u0441 \u0430\u0432\u0442\u043e\u0440\u0430\u043c\u0438:<\/p>\n<pre><code>import { createApi, fetchBaseQuery } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',     }),     getAuthors: builder.query({       query: () => '\/authors',     }),   }), });  export const { useGetPostsQuery, useGetAuthorsQuery } = postsApi;  const getPostsWithAuthors = async () => {   const [posts, authors] = await Promise.all([     postsApi.endpoints.getPosts(),     postsApi.endpoints.getAuthors(),   ]);    return posts.map((post) => ({     ...post,     author: authors.find((author) => author.id === post.authorId),   })); };  const customQueryFn = async (args, api, extraOptions) => {   const { data } = await getPostsWithAuthors();   return { data }; };  export const { useGetPostsWithAuthorsQuery } = postsApi.injectEndpoints({   endpoints: (builder) => ({     getPostsWithAuthors: builder.query({       queryFn: customQueryFn,     }),   }), });  function PostsWithAuthors() {   const { data: posts, isLoading, isError, error } = useGetPostsWithAuthorsQuery(); ...   return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>           {post.title} - {post.author.name}         &lt;\/li>       ))}     &lt;\/ul>   ); } <\/code><\/pre>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 createSelector<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 <code><strong>createApi<\/strong><\/code> \u0438\u0437 RTK Query, \u043e\u043d \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0432\u0430\u0441 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430. \u042d\u0442\u0438 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0441\u0442\u0435\u0439\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0435 \u0438\u043b\u0438 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430\u0445. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code><strong>createSelector<\/strong><\/code> \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code><strong>reselect<\/strong><\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code><strong>createSelector<\/strong><\/code> \u0441 RTK Query:<\/p>\n<pre><code>import { createSelector } from 'reselect'; import { useGetPostsQuery } from '.\/service';  const selectPosts = (state) => state.postsApi.posts; const selectAuthors = (state) => state.postsApi.authors;  const selectPostsAndAuthors = createSelector(   selectPosts,   selectAuthors,   (posts, authors) => {     return posts.map((post) => {       return {         ...post,         author: authors.find((author) => author.id === post.authorId),       };     });   } );  export default function Posts() {   const { isLoading: isLoadingPosts, isError: isErrorPosts, } = useGetPostsQuery(); const { isLoading: isLoadingAuthors, isError: isErrorAuthors, } = useGetAutorsQuery();   const postsAndAuthors = useSelector(selectPostsAndAuthors);    if (isLoadingPosts||isLoadingAuthors) {     return &lt;div>Loading...&lt;\/div>;   }    if (isErrorPosts||isErrorAuthors) {     return &lt;div>Error&lt;\/div>;   }    return (     &lt;ul>       {postsAndAuthors.map((post) => (         &lt;li key={post.id}>           &lt;div>{post.title}&lt;\/div>           &lt;div>Author: {post.author.name}&lt;\/div>         &lt;\/li>       ))}     &lt;\/ul>   ); } <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u0432\u0430 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430 <code><strong>selectPosts<\/strong><\/code> \u0438 <code><strong>selectAuthors<\/strong><\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 <code><strong>posts<\/strong><\/code> \u0438 <code><strong>authors<\/strong><\/code> \u0438\u0437 \u0441\u0442\u0435\u0439\u0442\u0430. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440 <code><strong>selectPostsAndAuthors<\/strong><\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code><strong>createSelector<\/strong><\/code>. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 <code><strong>selectPosts<\/strong><\/code> \u0438 <code><strong>selectAuthors<\/strong><\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0445 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code><strong>useSelector<\/strong><\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 <code><strong>postsAndAuthors<\/strong><\/code> \u0438\u0437 \u0441\u0442\u0435\u0439\u0442\u0430. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 JSX \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u0442\u043e\u0432 \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u0430\u0432\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code><strong>createSelector<\/strong><\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u043e\u0432, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 RTK Query.<\/p>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443 \u043d\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/www.npmjs.com\/package\/react-query\" rel=\"noopener noreferrer nofollow\">React Query<\/a>. \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 React-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443. \u041e\u043d\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e RTK Query.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 RTK Query \u0438 React Query. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, RTK Query \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Redux \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a React Query \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043d\u0435\u0433\u043e \u0438 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u0441 Redux, \u0442\u0430\u043a \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e. \u0422\u0430\u043a\u0436\u0435 RTK Query \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0443\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 API-\u0441\u0445\u0435\u043c\u044b \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c, RTK Query \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u0449\u043d\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u043a API \u0432 Redux-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u041e\u043d\u0430 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043a\u043e\u0434\u0430 \u0438 \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 Redux, \u0442\u043e RTK Query \u0441\u0442\u043e\u0438\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/730916\/\"> https:\/\/habr.com\/ru\/articles\/730916\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435 \u043c\u044b \u043d\u0435 \u043b\u044e\u0431\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u0445\u043e\u0442\u0438\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430) \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API &#8212; <a href=\"https:\/\/redux-toolkit.js.org\/rtk-query\/overview\" rel=\"noopener noreferrer nofollow\">RTK Query<\/a>. Redux \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043d\u0435\u0439, \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u043e\u0441\u0442\u0438 \u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, RTK query \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 Redux.<\/p>\n<h4>\u0417\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0438 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430<\/h4>\n<p>\u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043a API \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Redux \u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 RTK query. \u0421\u043d\u0430\u0447\u0430\u043b\u0430, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043a\u043e\u0434, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a API \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Redux \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u0442\u043e\u0432:<\/p>\n<pre><code> \/\/ actions.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043a\u0448\u0435\u043d\u043e\u0432 export const fetchPostsRequest = () => ({   type: 'FETCH_POSTS_REQUEST', });  export const fetchPostsSuccess = (posts) => ({   type: 'FETCH_POSTS_SUCCESS',   payload: posts, });  export const fetchPostsFailure = (error) => ({   type: 'FETCH_POSTS_FAILURE',   payload: error, });  \/\/ reducer.js \/\/ \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 Redux-\u0441\u0442\u0435\u0439\u0442\u0430 const initialState = {   posts: [],   isLoading: false,   error: null, };  \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0434\u044e\u0441\u0435\u0440\u043e\u0432 export default function postsReducer(state = initialState, action) {   switch (action.type) {     case 'FETCH_POSTS_REQUEST':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 true, \u043f\u043e\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f       return { ...state, isLoading: true };     case 'FETCH_POSTS_SUCCESS':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 false \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u0442\u0435\u0439\u0442       return { ...state, isLoading: false, posts: action.payload };     case 'FETCH_POSTS_FAILURE':       \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c isLoading \u0432 false \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0432 \u0441\u0442\u0435\u0439\u0442       return { ...state, isLoading: false, error: action.payload };     default:       return state;   } }  \/\/ thunk.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 import { fetchPostsRequest, fetchPostsSuccess, fetchPostsFailure } from '.\/actions';  export const fetchPosts = () => async (dispatch) => {   dispatch(fetchPostsRequest());   try {     const response = await fetch('\/api\/posts');     const data = await response.json();     dispatch(fetchPostsSuccess(data));   } catch (error) {     dispatch(fetchPostsFailure(error.message));   } };  \/\/selectors.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430 export const postsSelector = state => state.posts  \/\/ component.js import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { fetchPosts } from '.\/thunk'; import { postsSelector } from '.\/selectors.js'  export default function Posts() {   const dispatch = useDispatch();   const posts = useSelector(postsSelector);   const isLoading = useSelector((state) => state.isLoading);   const error = useSelector((state) => state.error);    useEffect(() => {     \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438 \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430     dispatch(fetchPosts());   }, [dispatch]);    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (error) {     return &lt;div>Error: {error}&lt;\/div>;   }    return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>{post.title}&lt;\/li>       ))}     &lt;\/ul>   ); }  <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 RTK query:<\/p>\n<pre><code>\/\/ service.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',     }),   }), });  export const { useGetPostsQuery } = postsApi;  \/\/ component.js import React, { useEffect } from 'react'; import { useGetPostsQuery } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery();    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (isError) {     return &lt;div>Error: {error.data}&lt;\/div>;   }    return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>{post.title}&lt;\/li>       ))}     &lt;\/ul>   ); }  <\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0431\u0435\u0437 RTK Query \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043a\u0448\u0435\u043d\u043e\u0432, \u0440\u0435\u0434\u044e\u0441\u0435\u0440\u043e\u0432, thunk-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430 \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 Redux \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a API \u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query \u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e <code>useGetPostsQuery<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u0442\u043e\u0432.<\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0438 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. RTK Query \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h4>\u041a\u044d\u0448\u0435\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u044d\u0433\u0438<\/h4>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432 \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 RTK Query \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u043f\u043e\u0441\u0442\u0430:<\/p>\n<pre><code>\/\/ service.js import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   tagTypes: ['Posts',],   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',       providesTags: ['Post'],     }),     addPost: builder.mutation({       query: (body) => ({         url: '\/posts',         method: 'POST',         body,       }), \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0430        invalidatesTags: ['Posts'],     }),   }), });  export const { useGetPostsQuery, useAddPostMutation } = postsApi;  \/\/ component.js import React, { useEffect, useState } from 'react'; import { useGetPostsQuery, useAddPostMutation } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery();   const [title, setTitle] = useState('');   const [body, setBody] = useState('');   const [addPost, { isError: addError }] = useAddPostMutation();    const handleAddPost = async () => {     await addPost({ title, body });     setTitle('');     setBody('');   };    if (isLoading) {     return &lt;div>Loading...&lt;\/div>;   }    if (isError || addError) {     return &lt;div>Error: {error?.data || addError?.data}&lt;\/div>;   }    return (     &lt;div>       &lt;form onSubmit={handleAddPost}>         &lt;input type=\"text\" placeholder=\"Title\" value={title} onChange={(e) => setTitle(e.target.value)} \/>         &lt;textarea placeholder=\"Body\" value={body} onChange={(e) => setBody(e.target.value)} \/>         &lt;button type=\"submit\">Add Post&lt;\/button>       &lt;\/form>       &lt;ul>         {posts.map((post) => (           &lt;li key={post.id}>{post.title}&lt;\/li>         ))}       &lt;\/ul>     &lt;\/div>   ); }  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a. \u0417\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0442\u044d\u0433\u0438. \u0422\u0435\u0433\u0438 \u0432 RTK query \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u044d\u0448\u0435\u043c. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>providesTags<\/code> \u0438 <code>invalidatesTags<\/code> \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 <code>query<\/code> \u0438 <code>mutation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c RTK query, \u043a\u0430\u043a\u0438\u0435 \u0442\u0435\u0433\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u043c\u0438 \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 \u044d\u0442\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438.<\/p>\n<p>\u0422\u0435\u0433\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>providesTags<\/code>, \u0431\u0443\u0434\u0443\u0442 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043f\u043e\u0441\u0442\u043e\u0432, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>getPosts<\/code>. \u0422\u0435\u0433\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>invalidatesTags<\/code>, \u0431\u0443\u0434\u0443\u0442 \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>addPost<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0441\u0442\u043e\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0430\u043c\u0438\u043c \u0432 \u043d\u0443\u0436\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a:<\/p>\n<pre><code>&lt;Button onClick={()=>postsApi.invalidateQueries(\"Post\")}> Update &lt;\/Button> <\/code><\/pre>\n<h4>transformResponse<\/h4>\n<p><code><strong>transformResponse<\/strong><\/code> &#8212; \u044d\u0442\u043e \u043e\u043f\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u0438\u0437 \u0441\u0435\u0442\u0438, \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u043e\u043c. \u041e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438\u0442.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0446\u0438\u044e <code><strong>transformResponse<\/strong><\/code> \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 <code><strong>date<\/strong><\/code> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043f\u0438\u0441\u043a\u0430:<\/p>\n<pre><code>\/\/ service.js \/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RTK Query import { createApi } from '@reduxjs\/toolkit\/query';  export const postsApi = createApi({   reducerPath: 'postsApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getPosts: builder.query({       query: () => '\/posts',       transformResponse: (response) => {         \/\/ \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0443         const posts = response.map((post) => ({           ...post,           date: new Date(),         }));         return posts;       },     }),   }), });  export const { useGetPostsQuery } = postsApi;  \/\/ component.js import React, { useEffect } from 'react'; import { useGetPostsQuery } from '.\/service';  export default function Posts() {   const { data: posts, isLoading, isError, error } = useGetPostsQuery(); ...   return (     &lt;ul>       {posts.map((post) => (         &lt;li key={post.id}>           {post.title} - {post.date.toLocaleString()}         &lt;\/li>       ))}     &lt;\/ul>   ); } <\/code><\/pre>\n<h4>queryFn<\/h4>\n<p><code><strong>queryFn<\/strong><\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u043b\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440: \u043e\u0434\u0438\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u043e\u0432, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u044d\u0442\u0438\u0445 \u043f\u043e\u0441\u0442\u043e\u0432. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code><strong>queryFn<\/strong><\/code> \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>\/\/ service.js import { createApi } from '@reduxjs\/toolkit\/query';  export const blogApi = createApi({   reducerPath: 'blogApi',   baseQuery: fetchBaseQuery({ baseUrl: '\/api' }),   endpoints: (builder) => ({     getBlogData: builder.query({       queryFn: async () => {         const [posts, authors] = await Promise.all([           fetch('\/api\/posts').then((res) => res.json()),           fetch('\/api\/authors').then((res) => res.json()),         ]);         return { posts, authors };       },       transformResponse: (response) => {         const { posts, authors } = response;         const authorsById = {};         authors.forEach((author) => {           authorsById[author.id] = author;         });         const postsWithAuthors = posts.map((post) => ({           ...post,           author: authorsById[post.authorId],         }));         return postsWithAuthors;       },     }),   }), });  export const { useGetBlogDataQuery } = blogApi;  \/\/ component.js import React from 'react'; import { useGetBlogDataQuery } from<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-395558","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/395558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=395558"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/395558\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}