{"id":369190,"date":"2024-05-21T03:58:06","date_gmt":"2024-05-21T03:58:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=369190"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=369190","title":{"rendered":"<span>\u0417\u0430\u043f\u0440\u043e\u0441\u044b, \u0445\u0443\u043a\u0438 \u0438 \u0441\u043f\u0430\u0433\u0435\u0442\u0442\u0438<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043c\u044b \u043f\u043e\u0440\u043e\u0439 \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043d\u0443\u0442\u0440\u0438 useEffect \u043f\u0440\u044f\u043c\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0441 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u043c. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044e \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u044b.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u0430\u0448\u0438\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445, \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<h3>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 useEffect \u043f\u0440\u044f\u043c\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445:<\/h3>\n<ol>\n<li>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u042d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0440\u0443\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u0435\u043d\u0435\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0447\u0430\u0441\u0442\u044f\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0434 \u0441\u043e \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u043e\u0439 \u0441 \u0432\u0438\u0437\u0443\u0430\u043b\u043e\u043c \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c:<\/p>\n<p><strong>\/YourComponent.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import React, { useEffect, useState } from 'react'; import axios from 'axios';  const YourComponent = () => {   const [todoId] = useState('1');    const [todoRequest, setTodoRequest] = useState({     data: null,     isLoading: false,     isError: false   });    const [postId] = useState('1');    const [postsRequest, setPostsRequest] = useState({     data: null,     isLoading: false,     isError: false   });    useEffect(() => {     const fetchTodos = async () => {       try {         setTodoRequest((prev)=>({...prev, isLoading: true}));         const response = await axios.get(`https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`);         setTodoRequest((prev)=>({...prev, data: response.data, isLoading: false}));       } catch (error) {         setTodoRequest((prev)=>({...prev, isLoading: false, isError: true}));       }     };      fetchTodos();   }, [todoId]);    useEffect(() => {     const fetchPosts = async () => {       try {         setPostsRequest((prev)=>({...prev, isLoading: true}));         const response = await axios.get(`https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`);         setPostsRequest((prev)=>({...prev, data: response.data, isLoading: false}));       } catch (error) {         setPostsRequest((prev)=>({...prev, isLoading: false, isError: true}));       }     };      fetchPosts();   }, [postId]);    return (     &lt;div>       {todoRequest.isLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{todoRequest.data &amp;&amp; JSON.stringify(todoRequest.data, null, 2)}&lt;\/pre>       {todoRequest.isError &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438&lt;\/h1>}        {postsRequest.isLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{postsRequest.data &amp;&amp; JSON.stringify(postsRequest.data, null, 2)}&lt;\/pre>       {postsRequest.isError &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438&lt;\/h1>}     &lt;\/div>   ); };  export default YourComponent;<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e, \u0438 \u0441 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u0441\u043f\u0430\u0433\u0435\u0442\u0442\u0438.<\/p>\n<h3>\u041a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f\u0441\u044c \u043e\u0442 useEffect?<\/h3>\n<p>\u0412\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0445\u0443\u043a\u0438!<\/p>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0445\u0443\u043a\u0438, \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u043a\u043e\u0434\u0430, \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0445\u0443\u043a \u2014 <strong>useFetch:<\/strong><\/p>\n<p><strong>\/hooks\/useFetch.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useState, useEffect } from \"react\";  export const useFetch = ({ requestDeps, requestFn }) => {   const [request, setRequest] = useState({     data: null,     isLoading: false,     isError: false,   });    useEffect(() => {     const fetchPosts = async () => {       try {         setRequest((prev) => ({ ...prev, isLoading: true }));         const response = await requestFn();         setRequest((prev) => ({           ...prev,           data: response.data,           isLoading: false,         }));       } catch (error) {         console.log(error);         setRequest((prev) => ({           ...prev,           isLoading: false,           isError: true,         }));       }     };      fetchPosts();   }, [...requestDeps]);    return {     data: request.data,     isLoading: request.isLoading,     isError: request.isError,   }; };<\/code><\/pre>\n<p><strong>useFetch<\/strong> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441:<\/p>\n<ol>\n<li>\n<p>\u0414\u0430\u043d\u043d\u044b\u043c\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0432\u0437\u044f\u0432 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 <strong>useFetch,<\/strong> \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0445\u0443\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u0430 \u0438 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<p><strong>\/hooks\/usePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useFetch } from \".\/useFetch\"; import axios from \"axios\";  export const usePost = ({ postId }) => {   return useFetch({     requestDeps: [postId],     requestFn: async () => {       const data = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><strong>\/hooks\/useTodo.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useFetch } from \".\/useFetch\"; import axios from \"axios\";  export const useTodo = ({ todoId }) => {   return useFetch({     requestDeps: [todoId],     requestFn: async () => {       const data = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0445\u0443\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435!<\/p>\n<p><strong>\/YourComponent.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import React from \"react\"; import { usePost } from \".\/hooks\/usePost\"; import { useTodo } from \".\/hooks\/useTodo\";  const YourComponent = () => {   const {     data: postData,     isLoading: isPostLoading,     isError: isPostFailed,   } = usePost({ postId: \"1\" });    const {     data: todoData,     isLoading: isTodoLoading,     isError: isTodoFailed,   } = useTodo({ todoId: \"1\" });    return (     &lt;div>       {isTodoLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{todoData &amp;&amp; JSON.stringify(todoData, null, 2)}&lt;\/pre>       {isTodoFailed &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438&lt;\/h1>}        {isPostLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{postData &amp;&amp; JSON.stringify(postData, null, 2)}&lt;\/pre>       {isPostFailed &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438&lt;\/h1>}     &lt;\/div>   ); };  export default YourComponent;<\/code><\/pre>\n<p>\u041a\u043e\u0434\u0430 \u0432 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0441\u0442\u0430\u043b\u043e \u0432 2 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435, \u0438 \u043b\u043e\u0433\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 React Query \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/h3>\n<p>React Query &#8212; \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \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 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041e\u043d\u0430 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a.<\/p>\n<p><u>\u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/installation\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/installation<\/a><\/p>\n<p>React Query \u0442\u0430\u043a\u0436\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 useEffect \u0438 \u0434\u0430\u0451\u0442 \u043e\u0431\u0448\u0438\u0440\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0443\u0436\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c React Query:<\/p>\n<p>useFetch \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0437\u0430 \u043d\u0435\u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e, useQuery \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u0437\u0430 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0430\u0441.<\/p>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442 react-query:<\/p>\n<p><strong>\/App.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { QueryClient, QueryClientProvider } from \"@tanstack\/react-query\"; import YourComponent from \".\/app\/YourComponent\";  const queryClient = new QueryClient({   defaultOptions: {     queries: {       staleTime: 10000,     },   }, });  export default function App() {   return (     &lt;QueryClientProvider client={queryClient}>       &lt;YourComponent \/>     &lt;\/ QueryClientProvider>   ); }<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <strong>QueryClient<\/strong> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0434\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b useQuery. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0437\u0430\u0434\u0430\u043b\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e staleTime \u0440\u0430\u0432\u043d\u044b\u0439 10000.<\/p>\n<p><strong>staleTime<\/strong> &#8212; \u0432\u0440\u0435\u043c\u044f \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c\u0438. \u041f\u0440\u0438 \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0445\u0443\u043a\u0430\u043c \u0441\u0432\u043e\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f staleTime.<\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e QueryClient:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient<\/a><\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c useQuery \u0432 \u043d\u0430\u0448\u0438\u0445 \u0445\u0443\u043a\u0430\u0445:<\/p>\n<p><strong>\/hooks\/usePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import axios from \"axios\"; import { useQuery } from \"@tanstack\/react-query\";  export const usePost = ({ postId }) => {   return useQuery({     queryKey: [\"post\", postId],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><strong>\/hooks\/useTodo.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import axios from \"axios\"; import { useQuery } from \"@tanstack\/react-query\";  export const useTodo = ({ todoId }) => {   return useQuery({     queryKey: [\"todo\", todoId],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><em>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 YourComponent \u043d\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a useFetch \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 useQuery \u043f\u043e\u043b\u044f.<\/em><\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e useQuery:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useQuery\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useQuery<\/a><\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c invalidateQueries \u0432 React Query<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 POST \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0441\u0442\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043f\u043e\u0441\u0442\u043e\u0432.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e React Query \u043d\u0435 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u044f \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e useEffect \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0445\u0443\u043a <strong>usePosts<\/strong>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u044b:<\/p>\n<p><strong>\/hooks\/usePosts.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useQuery } from \"@tanstack\/react-query\"; import axios from \"axios\";  export const usePosts = () => {   return useQuery({     queryKey: [\"posts\"],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/`,       );       return data;     },   }); };<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0445\u0443\u043a <strong>useCreatePost<\/strong>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u044b:<\/p>\n<p><strong>\/hooks\/useCreatePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useMutation, useQueryClient } from \"@tanstack\/react-query\"; import axios from \"axios\";  export const useCreatePost = () => {   const queryClient = useQueryClient();   return useMutation({     mutationKey: [\"create-post\"],     mutationFn: async ({ postTitle }) => {       const { data } = await axios.post(         `https:\/\/jsonplaceholder.typicode.com\/posts`,         {           title: postTitle,         },       );       return data;     },     onSuccess: () => {       queryClient.invalidateQueries({ queryKey: [\"posts\"] });     },   }); };<\/code><\/pre>\n<p><strong>useMutation<\/strong> &#8212; \u0445\u0443\u043a, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0443\u0442\u0430\u0446\u0438\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435, \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043c\u0443\u0442\u0430\u0446\u0438\u0439.<\/p>\n<p><strong>queryClient.invalidateQueries<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 (\u043e\u0447\u0438\u0441\u0442\u043a\u0438) \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 React Query \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430 \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0438\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u043f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u0434\u0430\u043d\u043d\u044b\u043c. \u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0412 \u043c\u0430\u0441\u0441\u0438\u0432 <strong>queryKey<\/strong> \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043a\u043b\u044e\u0447\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u043e\u0442\u0438\u043c \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u043c\u0443\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e useMutation:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useMutation\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useMutation<\/a><\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e queryClient.invalidateQueries:<\/u><strong> <\/strong><a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient#queryclientinvalidatequeries\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient#queryclientinvalidatequeries<\/a><\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0443\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435:<\/p>\n<p><strong>\/YourComponent.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import React, { useState } from \"react\"; import { useCreatePost } from \".\/hooks\/useCreatePost\"; import { usePosts } from \".\/hooks\/usePosts\";  const YourComponent = () => {   const [postTitle, setPostTitle] = useState(\"\");   const {     data: postsData,     isLoading: isPostsLoading,     isError: isPostsFailed,   } = usePosts();    const createPost = useCreatePost();    const submitPost = () =>     createPost.mutate({       postTitle,     });    return (     &lt;div>       &lt;input value={postTitle} onChange={(e) => setPostTitle(e.target.value)} \/>       &lt;button onClick={submitPost} disabled={!postTitle.length}>         \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0441\u0442       &lt;\/button>       {isPostsLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{postsData &amp;&amp; JSON.stringify(postsData, null, 2)}&lt;\/pre>       {isPostsFailed &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438&lt;\/h1>}     &lt;\/div>   ); };  export default YourComponent;<\/code><\/pre>\n<h3>\u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0443\u043a\u0430 use \u0432 React 19<\/h3>\n<p>\u0412 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 React \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f useEffect!<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <strong>use<\/strong> \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">import { use } from 'react';  function MessageComponent({ messagePromise }) {   const message = use(messagePromise);   const theme = use(ThemeContext);   \/\/ ...<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e <strong>use<\/strong> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/react.dev\/reference\/react\/use\" rel=\"noopener noreferrer nofollow\">\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0439\u0442\u0435 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u0432\u0430\u0448\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0441\u043f\u0430\u0433\u0435\u0442\u0442\u0438 \u0438 \u043a\u0443\u0447\u0438 \u043e\u0448\u0438\u0431\u043e\u043a. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0439\u0442\u0435 \u0447\u0438\u0441\u0442\u043e\u0442\u0443 \u043a\u043e\u0434\u0430, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439\u0442\u0435 \u043b\u043e\u0433\u0438\u043a\u0443, \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u0439\u0442\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u0435 \u0437\u0430 \u0447\u0435\u0442\u043a\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<\/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\/796143\/\"> https:\/\/habr.com\/ru\/articles\/796143\/<\/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>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043c\u044b \u043f\u043e\u0440\u043e\u0439 \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043d\u0443\u0442\u0440\u0438 useEffect \u043f\u0440\u044f\u043c\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0441 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u043c. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044e \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u044b.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u0430\u0448\u0438\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445, \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<h3>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 useEffect \u043f\u0440\u044f\u043c\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445:<\/h3>\n<ol>\n<li>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u042d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0440\u0443\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u0435\u043d\u0435\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0447\u0430\u0441\u0442\u044f\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0434 \u0441\u043e \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u043e\u0439 \u0441 \u0432\u0438\u0437\u0443\u0430\u043b\u043e\u043c \u043b\u043e\u0433\u0438\u043a\u043e\u0439, \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c:<\/p>\n<p><strong>\/YourComponent.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import React, { useEffect, useState } from 'react'; import axios from 'axios';  const YourComponent = () => {   const [todoId] = useState('1');    const [todoRequest, setTodoRequest] = useState({     data: null,     isLoading: false,     isError: false   });    const [postId] = useState('1');    const [postsRequest, setPostsRequest] = useState({     data: null,     isLoading: false,     isError: false   });    useEffect(() => {     const fetchTodos = async () => {       try {         setTodoRequest((prev)=>({...prev, isLoading: true}));         const response = await axios.get(`https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`);         setTodoRequest((prev)=>({...prev, data: response.data, isLoading: false}));       } catch (error) {         setTodoRequest((prev)=>({...prev, isLoading: false, isError: true}));       }     };      fetchTodos();   }, [todoId]);    useEffect(() => {     const fetchPosts = async () => {       try {         setPostsRequest((prev)=>({...prev, isLoading: true}));         const response = await axios.get(`https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`);         setPostsRequest((prev)=>({...prev, data: response.data, isLoading: false}));       } catch (error) {         setPostsRequest((prev)=>({...prev, isLoading: false, isError: true}));       }     };      fetchPosts();   }, [postId]);    return (     &lt;div>       {todoRequest.isLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{todoRequest.data &amp;&amp; JSON.stringify(todoRequest.data, null, 2)}&lt;\/pre>       {todoRequest.isError &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438&lt;\/h1>}        {postsRequest.isLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{postsRequest.data &amp;&amp; JSON.stringify(postsRequest.data, null, 2)}&lt;\/pre>       {postsRequest.isError &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438&lt;\/h1>}     &lt;\/div>   ); };  export default YourComponent;<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e, \u0438 \u0441 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u0441\u043f\u0430\u0433\u0435\u0442\u0442\u0438.<\/p>\n<h3>\u041a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f\u0441\u044c \u043e\u0442 useEffect?<\/h3>\n<p>\u0412\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0445\u0443\u043a\u0438!<\/p>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0445\u0443\u043a\u0438, \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u043a\u043e\u0434\u0430, \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0445\u0443\u043a \u2014 <strong>useFetch:<\/strong><\/p>\n<p><strong>\/hooks\/useFetch.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useState, useEffect } from \"react\";  export const useFetch = ({ requestDeps, requestFn }) => {   const [request, setRequest] = useState({     data: null,     isLoading: false,     isError: false,   });    useEffect(() => {     const fetchPosts = async () => {       try {         setRequest((prev) => ({ ...prev, isLoading: true }));         const response = await requestFn();         setRequest((prev) => ({           ...prev,           data: response.data,           isLoading: false,         }));       } catch (error) {         console.log(error);         setRequest((prev) => ({           ...prev,           isLoading: false,           isError: true,         }));       }     };      fetchPosts();   }, [...requestDeps]);    return {     data: request.data,     isLoading: request.isLoading,     isError: request.isError,   }; };<\/code><\/pre>\n<p><strong>useFetch<\/strong> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441:<\/p>\n<ol>\n<li>\n<p>\u0414\u0430\u043d\u043d\u044b\u043c\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0432\u0437\u044f\u0432 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 <strong>useFetch,<\/strong> \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0445\u0443\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u0430 \u0438 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<p><strong>\/hooks\/usePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useFetch } from \".\/useFetch\"; import axios from \"axios\";  export const usePost = ({ postId }) => {   return useFetch({     requestDeps: [postId],     requestFn: async () => {       const data = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><strong>\/hooks\/useTodo.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useFetch } from \".\/useFetch\"; import axios from \"axios\";  export const useTodo = ({ todoId }) => {   return useFetch({     requestDeps: [todoId],     requestFn: async () => {       const data = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0445\u0443\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435!<\/p>\n<p><strong>\/YourComponent.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import React from \"react\"; import { usePost } from \".\/hooks\/usePost\"; import { useTodo } from \".\/hooks\/useTodo\";  const YourComponent = () => {   const {     data: postData,     isLoading: isPostLoading,     isError: isPostFailed,   } = usePost({ postId: \"1\" });    const {     data: todoData,     isLoading: isTodoLoading,     isError: isTodoFailed,   } = useTodo({ todoId: \"1\" });    return (     &lt;div>       {isTodoLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{todoData &amp;&amp; JSON.stringify(todoData, null, 2)}&lt;\/pre>       {isTodoFailed &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438&lt;\/h1>}        {isPostLoading &amp;&amp; &lt;p>Loading...&lt;\/p>}       &lt;pre>{postData &amp;&amp; JSON.stringify(postData, null, 2)}&lt;\/pre>       {isPostFailed &amp;&amp; &lt;h1>\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438&lt;\/h1>}     &lt;\/div>   ); };  export default YourComponent;<\/code><\/pre>\n<p>\u041a\u043e\u0434\u0430 \u0432 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0441\u0442\u0430\u043b\u043e \u0432 2 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435, \u0438 \u043b\u043e\u0433\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 React Query \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/h3>\n<p>React Query &#8212; \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \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 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041e\u043d\u0430 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a.<\/p>\n<p><u>\u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/installation\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/installation<\/a><\/p>\n<p>React Query \u0442\u0430\u043a\u0436\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 useEffect \u0438 \u0434\u0430\u0451\u0442 \u043e\u0431\u0448\u0438\u0440\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0443\u0436\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c React Query:<\/p>\n<p>useFetch \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0437\u0430 \u043d\u0435\u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e, useQuery \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u0437\u0430 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0430\u0441.<\/p>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442 react-query:<\/p>\n<p><strong>\/App.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { QueryClient, QueryClientProvider } from \"@tanstack\/react-query\"; import YourComponent from \".\/app\/YourComponent\";  const queryClient = new QueryClient({   defaultOptions: {     queries: {       staleTime: 10000,     },   }, });  export default function App() {   return (     &lt;QueryClientProvider client={queryClient}>       &lt;YourComponent \/>     &lt;\/ QueryClientProvider>   ); }<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <strong>QueryClient<\/strong> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0434\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b useQuery. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0437\u0430\u0434\u0430\u043b\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e staleTime \u0440\u0430\u0432\u043d\u044b\u0439 10000.<\/p>\n<p><strong>staleTime<\/strong> &#8212; \u0432\u0440\u0435\u043c\u044f \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c\u0438. \u041f\u0440\u0438 \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0445\u0443\u043a\u0430\u043c \u0441\u0432\u043e\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f staleTime.<\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e QueryClient:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/reference\/QueryClient<\/a><\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c useQuery \u0432 \u043d\u0430\u0448\u0438\u0445 \u0445\u0443\u043a\u0430\u0445:<\/p>\n<p><strong>\/hooks\/usePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import axios from \"axios\"; import { useQuery } from \"@tanstack\/react-query\";  export const usePost = ({ postId }) => {   return useQuery({     queryKey: [\"post\", postId],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/${postId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><strong>\/hooks\/useTodo.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import axios from \"axios\"; import { useQuery } from \"@tanstack\/react-query\";  export const useTodo = ({ todoId }) => {   return useQuery({     queryKey: [\"todo\", todoId],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId}`,       );       return data;     },   }); };<\/code><\/pre>\n<p><em>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 YourComponent \u043d\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a useFetch \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 useQuery \u043f\u043e\u043b\u044f.<\/em><\/p>\n<p><u>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e useQuery:<\/u> <a href=\"https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useQuery\" rel=\"noopener noreferrer nofollow\">https:\/\/tanstack.com\/query\/latest\/docs\/framework\/react\/reference\/useQuery<\/a><\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c invalidateQueries \u0432 React Query<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 POST \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0441\u0442\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043f\u043e\u0441\u0442\u043e\u0432.<\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e React Query \u043d\u0435 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u044f \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e useEffect \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0445\u0443\u043a <strong>usePosts<\/strong>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u044b:<\/p>\n<p><strong>\/hooks\/usePosts.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useQuery } from \"@tanstack\/react-query\"; import axios from \"axios\";  export const usePosts = () => {   return useQuery({     queryKey: [\"posts\"],     queryFn: async () => {       const { data } = await axios.get(         `https:\/\/jsonplaceholder.typicode.com\/posts\/`,       );       return data;     },   }); };<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0445\u0443\u043a <strong>useCreatePost<\/strong>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u044b:<\/p>\n<p><strong>\/hooks\/useCreatePost.jsx<\/strong><\/p>\n<pre><code class=\"javascript\">import { useMutation, useQueryClient } from \"@tanstack\/react-query\"; import axios from \"axios\";  export const useCreatePost = () => {   const queryClient = useQueryClient();   return useMutation({     mutationKey: [\"create-post\"],     mutationFn: async ({ postTitle }) => {       const { data } = await axios.post(         `https:\/\/jsonplaceholder.typicode.com\/posts`,         {           title: postTitle,         },       );       return data;     },     onSuccess: () => {       queryClient.invalidateQueries({ queryKey: [\"posts\"] });     },   }); };<\/code><\/pre>\n<p><strong>useMutation<\/strong> &#8212; \u0445\u0443\u043a, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439<\/p>\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-369190","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369190","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=369190"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369190\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}