React-Queryãéå§ããåã®åææ¡ä»¶ãããã¾ãã
ã» React ããã¯ã¨é¢æ°ã³ã³ãã¼ãã³ãã«é¢ããä¸ç´ã¬ãã«ã®ã¹ãã«ãä¸å¯æ¬ ã§ãã
ã» JavaScriptã§ã®REST APIã¨ãã¼ã¿åå¾ã®åºæ¬çãªç解ã
ã»ãã·ã³ã«Node.jsãã¤ã³ã¹ãã¼ã«ããã¦ãããã¨ã確èªãã¦ãã ããã
React-Queryã¨ã¯ï¼
React-Queryã¯ãReactããã³Next.jsã³ã¼ããã¼ã¹ç¨ã«æ§ç¯ãããã©ã¤ãã©ãªã§ããããããã¯ã¼ã¯ãªã¯ã¨ã¹ããä½æããéã«å¾ãã¹ããã¹ã¦ã®ãã¹ããã©ã¯ãã£ã¹ãçµã¿è¾¼ã¾ãã¦ãã¾ãã
Reactããã³Next.jsã¢ããªã§ã®ãµã¼ãã¼ç¶æ ã®åå¾ããã£ãã·ã¥ãåæãããã³æ´æ°ãç°¡åã«ãã Reactç¨ã®ãã¼ã¿åå¾ã©ã¤ãã©ãªã¨ããäºã§ãã
ããã¯ããã£ãã·ã¥ããã§ãããã¦ãµã¼ãã¼ãããã¼ã¿ãåæããæ¹æ³ãç°¡ç´ åããReact ã©ã¤ãã©ãªã§ãããReactã®ä¸è¶³ãã¦ãããã¼ã¿ãã§ããã©ã¤ãã©ãªã¨ãã¦ãã説æããã¾ãããããæè¡çãªç¨èªã§è¨ãã°ãReact ã¢ããªã±ã¼ã·ã§ã³ã§ä¸è¨ã®ãã¹ã¦ã®ããã»ã¹ãç°¡åã«å®è¡ã§ãã¾ãã
Reactèªä½ã«ã¯ããµã¼ãã¼ãããã¼ã¿ããã§ããããæ¹æ³ã«é¢ããæè¦ã¯ããã¾ããã
æãåºæ¬çãªã¢ããã¼ãã¯ãã³ã³ãã¼ãã³ããæåã« useEffectã«ãã¦ã³ããããã¨ãã«ãã©ã¦ã¶ã®ãã§ããAPIã使ç¨ãã次ã«useStateã使ç¨ãã¦å¿çã管çãããã¨ã§ãã
ãã®ããã»ã¹ã¯æ©è½ãã¾ããããã£ãã·ã¥ãå試è¡ãéè¤æé¤ãªã©ã®è¦ä»¶ãããå ´åã§ãã
ãã¼ã¿åå¾ã³ã¼ããç°¡ç´ åããã ãã§ãªãããããã®è¤éãªè¦ä»¶ãããã«å¦çã§ãã¾ãã
ã¤ã¾ããReact-Queryã使ç¨ããã¨ãã°ãã¼ãã«ãªç¶æ ãå¤æ´ãããã¨ãªããã·ã³ãã«ãã¤å®£è¨çãªæ¹æ³ã§Reactãã¼ã¹ã®ã¢ããªã±ã¼ã·ã§ã³ã®ãã¼ã¿ããã§ããããã£ãã·ã¥ãããã³æ´æ°ã§ãã¾ãã
React-Queryã¯ãReactã®ã¨ã³ã·ã¹ãã ã§æã人æ°ã®ããã©ã¤ãã©ãªã®1ã¤ã«ãªãã¤ã¤ããã¾ãã
ReduxãuseEffectã¨ã®éãã¯ï¼
React-Queryã¯ãAxiosãããfetch APIãã¨åãã§ã¯ãªãããReduxãã¾ãã¯ãContext APIãã®ä»£æ¿åã§ããªããã¨ãæ確ã«ããå¿ è¦ãããã¾ãã
React-Queryã¯ä¸»ã«ãã¼ã¿åæã«ä½¿ç¨ããããReduxãã¯ã°ãã¼ãã«ãªç¶æ 管çã«ä½¿ç¨ããã¾ãã
useEffectãªã©ã®ä¸è¬çãªãã¼ã¿åå¾ãã¿ã¼ã³ã¨ã®éãã¯ãReact-Queryã¯åååå¾ãããã¼ã¿ãè¿ãããã®å¾ããä¸åº¦åå¾ãç´ãã¨ããç¹ã§ãã
ãããªã½ã¼ã¹ãæåã¨åãã§ããã°ãReact-Queryã¯ãã¼ã¸ã®åèªã¿è¾¼ã¿ãå¼·å¶ãããã¨ãªãã両æ¹ã®ãã¼ã¿ãåç §ã¨ãã¦ä¿æè´ãã¾ãã
React-Queryã使ç¨ããçç±
ããã¯ãã¼ã¿ãåå¾ããã ããªã®ãï¼ç§ãã¡éçºè
ã¯ãJS FetchAPI
ã¾ãã¯Axios
ã¨useEffect
ããã³useState
ããã¯ã使ç¨ãã¦ããããè¡ã£ã¦ãã¾ããã
ããããçªç¶useEffect㨠useStateã®ä»£ããã« React-Query
ã使ç¨ããã®ã¯ãªããªã®ãï¼
ãåãããããã¾ããããã»ã¨ãã©ã®å¾æ¥ã®ç®¡çã©ã¤ãã©ãªã¯ã¯ã©ã¤ã¢ã³ãç¶æ ã®æä½ã«ã¯åªãã¦ãã¾ãããéåæã¾ãã¯ãµã¼ãã¼ç¶æ ã®æä½ã§ã¯ç ©ããããªãã¾ããã
React-Queryã使ç¨ãã主ãªçç±ã®1ã¤ã¯ãé »ç¹ãªæ´æ°ããã£ãã·ã¥ãããã³ãµã¼ãã¼ã¨ã®åæãå¿ è¦ãªéåæãã¼ã¿ãæ±ãå ´åãReact-Queryãé常ã«ä¾¿å©ã§ãããã¨ã§ãã
ãã ããReact-Queryããã®ãããªæ©è½ãæä¾ããããã«ã¯ããã®æ¦å¿µã使ãæ¹ã«ã¤ãã¦ç解ããå¿ è¦ãããã¾ãã
ãã®ãããReact-Queryã使ç¨ããåã«ããã®ããã¥ã¡ã³ãããã¥ã¼ããªã¢ã«ããã£ããã¨å¦ç¿ããé©åã«ä½¿ãããªããã¨ãéè¦ã§ãã
åè¿°ããéããReact-Queryã¯ãã°ãã¼ãã«ãªç¶æ ããªãã¦ããã¼ã¿ã®åå¾ããã£ãã·ã¥ãæ´æ°ãç°¡ç´ åã§ããããã使ãããããå¿ è¦ã«å¿ãã¦ã«ã¹ã¿ãã¤ãºã§ãã¾ãã
ããã«ãããå¾æ¥ã®æ¹æ³ãããå¹ççã«éåæãã¼ã¿ãå¦çã§ããããã«ãªãã¾ãã
React-Queryã使ç¨ãããã¼ã¿åå¾
CRAã§æ°ããReact JSããã¸ã§ã¯ããä½æããreact-query
ãã¤ã³ã¹ãã¼ã«ãã¾ãã
npm i react-query // or yarn add react-query
次ã«ãApp.js
ãã¡ã¤ã«ã§react-queryããuseQuery
ãã¤ã³ãã¼ããã¾ãã
// App.js import { useQuery } from "react-query";
åç´ãªfetchData
é¢æ°ãè¨è¿°ãã¦Rest APIãããã¼ã¿ããã§ãããã¾ãã
æ¬æ¥ã¯fetch()
ã使ç¨ãã¦ããã¾ãããAxiosããã®ä»ã®æ¹æ³ã§ãå¯è½ã§ãã
JSONãã¬ã¼ã¹ãã«ãã¼API ã«ãããã¯ã¼ã¯ãªã¯ã¨ã¹ããè¡ãasyncé¢æ°ãå®ç¾©ããå¿ è¦ãããã¾ãã
// App.js const fetchData = async () => { const res = await fetch("https://jsonplaceholder.typicode.com/users"); return res.json(); };
ããã¦useQueryããã¯ã使ç¨ãã¦ãä¸è¨ã®ããã«ãã¼ã¿åå¾ã管çã§ãã¾ãã
const response = useQuery("users", fetchData);
useQueryããã¯ã«ã¯2ã¤ã®å¼æ°ãå¿ è¦ã¨ãªãã¾ãã
1ã¤ã¯ãã®ã¯ã¨ãªã®key
ã§ãã
ãã®ããã«ãæååã®users
ã使ç¨ãã¦ãã¾ãã
æåã®å¼æ°ã¨ãã¦é åãç½®ããã¨ãã§ããé åã渡ãããå ´åã§ã¯åã¢ã¤ãã ã¯å®å®ããã¯ã¨ãªãã¼ã«ã·ãªã¢ã«åããã¾ãã
2ã¤ç®ã¯ããã¼ã¿ããã§ããããé¢æ°ã¨ãªãã¾ãã
å è¿°ã§ä½æããfetchDataã®asyncé¢æ°ã渡ãã¾ãã
ã¾ãããã¾ãã¾ãªãªãã·ã§ã³ã®3çªç®ã®å¼æ°ã¨ãã¦ãªãã¸ã§ã¯ãã渡ããã¨ãå¯è½ã§ãã
useQueryããè¿ãããå¿çã¯é常ã«éè¦ã¨ãªã£ã¦ãã¾ãã
ä¸è¨ã®ããããã£ãããã¾ãã
ã» data
ã» error
ã» failureCount
ã» isError
ã» isFetchedAfterMount
ã» isFetching
ã» isIdle
ã» isLoading
ã» isPreviousData
ã» isStale
ã» isSuccess
ã» refetch
ã» remove
ã» status
data
ã¯åå¾ããå®éã®ãã¼ã¿ã§ãã
status
ã¯ãå¿çã«å¿ãã¦loadingãerrorãsuccessãã¾ãã¯idleã«ãªãã¾ãã
ä¸è¨ããããã¹ã¦ã®ããããã£ã«ã¯ãã¾ãã¾ãªç¨éãããã¾ãã
æ¬æ¥ã¯ãdata
ã¨status
ã error
ããã³isError
ããããã£ã使ç¨ããä¾ã示ãã¾ãã
const { data, status } = useQuery("users", fetchData);
åè¿°ããããã«ãuseQueryããã¯ã¯2ã¤ã®å¼æ°ãåããæåã®å¼æ°ã¯ãã£ãã·ã¥ããæååã§ãããã¯ã¨ãªçµæã追跡ãã¾ãã
2çªç®ã®å¼æ°ã¯ãHTTPãªã¯ã¨ã¹ããä½æããããã«å®ç¾©ããfetchData
é¢æ°ã§ãã
React-Queryã¯ããã£ãã·ã¥ã¨ãã¼ã¿ã®æ´æ°ãå é¨çã«å¦çãã¾ãã
ããã§ããã¼ã¿ã使ç¨ãã¦ãã©ã¦ã¶ã«è¡¨ç¤ºããäºãã§ãã¾ãã
// App.js import { useQuery } from "react-query"; const fetchData = async () => { const res = await fetch("https://jsonplaceholder.typicode.com/users"); return res.json(); }; const App = () => { const { data, status } = useQuery("users", fetchData); return ( <div className="App"> {status === "error" && <p>Error fetching data</p>} {status === "loading" && <p>Fetching data...</p>} {status === "success" && ( <div> {data.map((user) => ( <p key={user.id}>{user.name}</p> ))} </div> )} </div> ); }; export default App;
ä¸è¨ã§è¡ã£ãäºã¯ãstatus
ã確èªãã¦ãã¼ã¿ã表示ãããã¨ã§ãã
ããã¯ãReact-Queryã®useQueryããã¯ã®ä½¿ç¨æ¹æ³ãç°¡åã«èª¬æãããã®ã¨ãªãã¾ãã
QueryClientProvider
åºæ¬çã«ã¯ãReact.jsã¢ããªã±ã¼ã·ã§ã³ã§React-Queryãæ§æããã«ã¯ããã¼ã¿åå¾ãå¿
è¦ã¨ããæä¸ä½ã®ã³ã³ãã¼ãã³ããQueryClientProvider
ã³ã³ãã¼ãã³ãã§ã©ããããå¿
è¦ãããã¾ãã
QueryClientã¯ãä»ã®ã³ã³ãã¼ãã³ãããã£ãã·ã¥ãå©ç¨ã§ããããã«ãã¾ãã
React-Queryãèµ·åããããã«ãã«ã¼ããã£ã¬ã¯ããªã®ãã¡ã¤ã«index.js
ã«ä¸è¨ã®ãããªåºæ¬çãªè¨å®ããã¾ãã
// index.js v18ã import React from 'react'; import { createRoot } from 'react-dom/client'; import 'index.css'; import App from './App'; import { QueryClient, QueryClientProvider } from "react-query"; const root = ReactDOM.createRoot(document.getElementById('root')); const queryClient = new QueryClient(); root.render( <React.ScriptMode> //ã©ãã <QueryClientProvider client={queryClient}> <App /> </QueryClientProvider> </React.ScriptMode> );
// index.js import React from 'react' import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import { QueryClient, QueryClientProvider } from "react-query"; const queryClient = new QueryClient(); ReactDOM.render( //ã©ãã <QueryClientProvider client={queryClient}> <App /> </QueryClientProvider>, document.getElementById('root') );
QueryClientProvider
ã®åã³ã³ãã¼ãã³ãã¯ãReact-Queryã©ã¤ãã©ãªãæä¾ããããã¯ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããQueryClient
ã¤ã³ã¹ã¿ã³ã¹ãæä¾ãããããã«ãªãã¾ããã
ãã®ã¤ã³ã¹ã¿ã³ã¹ã使ç¨ããReact-Queryã©ã¤ãã©ãªãæä¾ããããã¯ã«ã¢ã¯ã»ã¹ãããã¨ã«ãªãã¾ãã
ã¯ã¨ãªã¯ã©ã¤ã¢ã³ããã¤ã³ã¹ã¿ã³ã¹åããã³ã³ãã¼ãã³ãããªã¼ã«æä¾ããã¨ããã¹ã¦ã®åã³ã³ãã¼ãã³ããããã®ããã«ãã¼ã¿ããã§ããã§ããããã«ãªãã¾ãã
ããã¯éçºä¸ã®ãããã°ã«ä½¿ç¨ãããäºãå¤ãã§ãã
import {useQuery} from 'react-query'; const fetchData = async () => { const res = await fetch("https://jsonplaceholder.typicode.com/users"); return res.json(); }; function Users(){ const {data, status} = useQuery('users', fetchData) if(status === "Loading"){ return <div>Loading...</div> } if(status === "isError"){ return <div>Loadingâ¦</div> } return( <div className='container'> <h1>Users</h1> { data.map((user, index) => { return <li key={index}>{user.title}</li> }) } </div> ) } export default Users;
ã³ã³ãã¼ãã³ãã® return
ãã¼ã¯ã¼ãã®åã«ãåºæ¬çãªãã¸ãã¯ãå®è¡ããã¢ããªã±ã¼ã·ã§ã³ããã¼ãç¶æ
ã«ãããã©ãããã¾ãã¯ã¨ã©ã¼ãçºçãããã©ããã確èªãã¾ãã
useQueryããè¿ãããå¿çã«error
ã¨isError
ããããã£ã使ç¨ããã®ãåºæ¬ã§ãã
const {data, status, error, isError} = useQuery('users', fetchData) ; if(status === "Loading"){ return <div>Loading...</div> } if(isError){ return <div>Error! {error. message}</div> }
React-Queryã使ç¨ããã¨ãã¨ã©ã¼å¦çãç°¡åã«ãªãã¾ãã
ä¸è¨ã®éããReact-Queryã¯ãµã¼ãã¼ã®ç¶æ ã管çããããã®æè¯ã®ã©ã¤ãã©ãªã®1ã¤ã§ãã
ã¢ããªãã¼ã¿ãã¦ã¼ã¶ã¼ãå¶å¾¡ãå§ããåã«å¶å¾¡ãããã¨ãå¯è½ã¨ãªãã¾ãã
ã¯ã¨ãªã並åã§å®è¡ãããå ´åã¯ãuseQueryã並ã¹ã¦æ¸ãã°åé¡ããã¾ããã
ãã ããé åã«åºã¥ãã¦ã¯ã¨ãªãããå ´åããSuspenseã使ã£ã¦ããå ´åã¯useQueriesã使ãå¿ è¦ãããã¾ãã®ã§æ³¨æãã¦ä¸ããã
const userQueries = useQueries( users.map((user) => { return { queryKey: ['user', user.id], queryFn: () => fetchUserById(user.id), }; }), )``;
ããå°ãã詳ããå¦ã°ãããæ¹ã¯ä¸è¨ã¸é²ãã§ä¸ããã
ã¯ã¨ãª
ã¯ã¨ãªããã«ã¯ä¸è¨ã®ï¼ã¤ãå¿ è¦ã¨ãªãã¾ãã
ã» ã¦ãã¼ã¯ãªç¬èªã®Key
ã» Promise(ãã¼ã¿ãresolveããããã¾ãã¯ã¨ã©ã¼ãæãã)
const result = useQuery('users', fetchData); // or const result = useQuery({ queryKey: 'users', queryFn: fetchData, });
ã» isLoading or status === 'loading'
åæãã¼ãä¸
ã» isError or status === 'error'
ã¨ã©ã¼ãçºçããç¶æ
ã» error
ã¨ã©ã¼å 容
ã» isSuccess or status === 'success'
ãã¼ã¿åå¾ãæåããç¶æ
ã» isIdle or status === 'idle'
ã¯ã¨ãªãç¡å¹ãããç¶æ
ã» isFetching or isLoading
ãªãããã®éä¿¡ä¸ã§ãã
ã¯ã¨ãªãã¼
ã» ã¯ã¨ãªãã¼ã«åºã¥ãã¦ãã£ãã·ã¥ãè¡ãããã
ã» ã·ãªã¢ã©ã¤ãºå¯è½ãªå¤ãªããªãã§ãKeyã¨ãã¦ä½¿ç¨ã§ããã
useQuery('users', ...) // queryKey === ['users'] useQuery(['user', 5], ...) // queryKey === ['user', 5] useQuery(['user', 5, { preview: true }], ...) // queryKey === ['user', 5, { preview: true }]
ã¯ã¨ãªãç¹å®ã®idçã«åºã¥ãã¦è¡ããããã®ã§ãããªãã°ããã®å¤ããã¼ã¨ãã¦å«ãã¦ãããã¨ãéè¦ã§ãã
function Users({ userId }) { const result = useQuery(['users', userId], () => fetchDataById(userId)); }
ã¯ã¨ãªãå®è¡ããé¢æ°
ãã¼ã¿åå¾ã«å¤±æããæã¯å¿ ãã¨ã©ã¼ãæãããã¨ã§ãã
axios
ã¨ã¯ç°ãªããfetch
ã¯ããã©ã«ãã§ã¯ã¨ã©ã¼ãæããªãã®ã§æ³¨æããå¿
è¦ãããã¾ãã
import { useQuery } from 'react-query'; // React Queryã©ã¤ãã©ãªã®useQueryããã¯ã使ç¨ãã¦ãAPIããã¦ã¼ã¶ã¼ãã¼ã¿ãåå¾ããé¢æ°ãå®ç¾©ãã useQuery(['users', userId], async () => { // APIã¨ã³ããã¤ã³ãã«ã¢ã¯ã»ã¹ããã¬ã¹ãã³ã¹ãåå¾ããããã«ãfetché¢æ°ã使ç¨ãã const response = await fetch('/users/' + userId); // fetché¢æ°ã¯ããã©ã«ãã§ã¨ã©ã¼ãæããªããããã¬ã¹ãã³ã¹ãã¨ã©ã¼ã§ããå ´åã¯ãã¨ã©ã¼ãæããå¿ è¦ããã // ã¬ã¹ãã³ã¹ãã¨ã©ã¼ã§ãããã©ããã¯ãokããããã£ã確èªãããã¨ã§å¤æã§ãã if (!response.ok) { // ã¬ã¹ãã³ã¹ãã¨ã©ã¼ã§ããå ´åã¯ãã¨ã©ã¼ãæãã throw new Error('ãããã¯ã¼ã¯ã®ã¬ã¹ãã³ã¹ãè¯ãããã¾ããã'); } // ã¬ã¹ãã³ã¹ãæ£å¸¸ã§ããå ´åã¯ãJSONãã¼ã¿ã解æãã¦è¿ã return response.json(); });
ä¸è¨ã®ã³ã¼ãä¾ã¯ãReact-Queryã©ã¤ãã©ãªã®useQueryããã¯ã使ç¨ãã¦ãAPIããã¦ã¼ã¶ã¼ãã¼ã¿ãåå¾ããé¢æ°ãå®ç¾©ãã¦ãã¾ãã
é¢æ°å ã§ã¯ããfetchãé¢æ°ã使ç¨ãã¦APIã¨ã³ããã¤ã³ãã«ã¢ã¯ã»ã¹ããã¬ã¹ãã³ã¹ãåå¾ãã¾ãã
ãfetchãé¢æ°ã¯Promiseãè¿ããããã¬ã¹ãã³ã¹ã帰ã£ã¦ããã¾ã§å¾ æ©ããå¿ è¦ãããã¾ãã
ãã®å¾ãã¬ã¹ãã³ã¹ã®ãokããããã£ãã確èªãã¦ãã¬ã¹ãã³ã¹ãã¨ã©ã¼ã§ãªããã¨ã確èªãã¾ãã
ã¬ã¹ãã³ã¹ãã¨ã©ã¼ã®å ´åã¯ãã¨ã©ã¼ãæãã¦é¢æ°ãä¸æãã¾ãã
ã¬ã¹ãã³ã¹ãæ£å¸¸ãªå ´åã¯ãJSONãã¼ã¿ã解æãã¦è¿ãã¾ãã
ã¨ã©ã¼ãæãããã¨ã§ãããã°ã©ã ãåé¡ãèªèããé©åã«å¯¾å¦ã§ããããã«ãªãã¾ãã
ã¾ããReact-Queryã©ã¤ãã©ãªã«ãã£ã¦ãã£ãã·ã¥ããããã¼ã¿ãè¿ãããå¯è½æ§ãããããã常ã«APIãããã¼ã¿ãåå¾ããããã«ãé¢æ°ã渡ãå¿ è¦ãããã¾ãã
ããã¯ã°ã©ã¦ãã§ã®ãã¼ã¿åå¾ãã¦ã¼ã¶ã¼ã«éç¥
ã» isFetching
ã§ã¯ããããéä¿¡ã§true
ã¨ãªãã¾ãã
ã» idLoading
ã¯ååãã¼ã¿åå¾ã®æã ãtrue
ã¨ãªãã¾ãã
ã» ã¢ããªå
¨ä½ã§ã®éä¿¡ç¶æ
ãåå¾ãããå ´åã§ã¯useIsFetching
ã使ç¨ãã¦ä¸ããã
以ä¸ã®ã³ã¼ãã«ãReactã¢ããªå ã§ãã¼ã¿ãåå¾ãã¦ããå ´åã«ãåå¾ä¸ãã©ããã示ããã©ã°ã§ãããisFetchingãã¨ãååãã¼ã¿åå¾æãã©ããã示ããã©ã°ãisLoadingãã使ç¨ããæ¹æ³ã示ãã¾ãã
import React, { useState, useEffect } from "react"; function App() { // ãã¼ã¿ã管çããstate const [data, setData] = useState([]); // ãã¼ã¿åå¾ä¸ãã©ããã管çããstate const [isFetching, setIsFetching] = useState(false); // ååã®ãã¼ã¿åå¾ä¸ãã©ããã管çããstate const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchData = async () => { // ãã¼ã¿åå¾ä¸ã§ãããã¨ã示ããã©ã°ãè¨å®ãã setIsFetching(true); // ãã¼ã¿ãåå¾ãã const response = await fetch("https://example.com/data"); const data = await response.json(); // ãã¼ã¿ãæ´æ°ãã setData(data); // ãã¼ã¿åå¾ãå®äºãããã¨ã示ããã©ã°ãè¨å®ãã setIsFetching(false); setIsLoading(false); }; // ååã®ã¢ããªã®ã¬ã³ããªã³ã°æã«ãã¼ã¿ãåå¾ãã fetchData(); }, []); return ( <div> {isLoading ? ( // ååãã¼ã¿åå¾ä¸ã§ããã°Loading...ã表示ãã <p>Loading...</p> ) : ( // ãã¼ã¿ãUIã«è¡¨ç¤ºãã <ul> {data.map((item) => ( <li key={item.id}>{item.name}</li> ))} </ul> )} </div> ); }
ä¸è¨ã®ä¾ã§ã¯ãuseEffectããã¯ã使ç¨ãã¦ãååã®ã¢ããªã®ã¬ã³ããªã³ã°æã«ãã¼ã¿ãåå¾ãã¦ãã¾ãã
ãã®ã¨ãããisLoadingããã©ã°ãtrue
ã«è¨å®ãã¦ãååã®ãã¼ã¿åå¾ãè¡ããããã¨ã示ãã¾ãã
ãã¼ã¿ã®åå¾ä¸ã«ã¯ããisFetchingããã©ã°ãtrue
ã«è¨å®ãããã¼ã¿ã®åå¾ãå®äºããããfalseãã«æ»ãã¾ãã
ããã«ãããUIãåå¾ä¸ã§ãããã¨ãã¦ã¼ã¶ã¼ã«éç¥ãããã¨ãã§ãã¾ãã
æå¾ã«ããisLoadingããã©ã°ãfalse
ã«è¨å®ãããã¨ã§ãååã®ãã¼ã¿åå¾ãå®äºãããã¨ã示ãããã¼ã¿ãUIã«è¡¨ç¤ºãããã¨ãã§ãã¾ãã
ã¾ããã¢ããªå ¨ä½ã§ã®éä¿¡ç¶æ ãç¥ãããå ´åã«ã¯ãreact-queryã©ã¤ãã©ãªã®ãuseIsFetchingãããã¯ã使ç¨ãããã¨ã§ãç¾å¨ã¢ã¯ãã£ããªAPIãªã¯ã¨ã¹ãã®æ°ãåå¾ãããã¨ãã§ãã¾ãã
以ä¸ã®ããã«importãã¦ä½¿ç¨ãããã¨ãã§ãã¾ãã
import { useIsFetching } from "react-query";
Mutations
ãã¼ã¿ã®ä½æã»æ´æ°ã»åé¤ããå ´åã¯useQueryã§ã¯ãªãuseMutation
ããã¯ã使ç¨ãã¾ãã
// useMutationããã¯ã使ç¨ãã¦ããã¼ã¿ãä½æã»æ´æ°ã»åé¤ããæ¹æ³ // `useMutation`ããã¯ã¯ãaxiosãªã©ã®HTTPã¯ã©ã¤ã¢ã³ãã使ç¨ãã¦APIãå¼ã³åºããã¨ãã§ãã¾ãã // useMutationããã¯ãã¤ã³ãã¼ããã¾ã import { useMutation } from 'react-query'; // useMutationããã¯ã使ç¨ãã¦ãAPIå¼ã³åºããå®è¡ãã¾ã const { mutate, isLoading, isError, isSuccess, error, data } = useMutation( (newUser) => axios.post('/users', newUser) // ãã¼ã¿ãä½æããå ´åã¯POSTãªã¯ã¨ã¹ãã使ç¨ãã¾ã // ãã¼ã¿ãæ´æ°ããå ´åã¯PUTãªã¯ã¨ã¹ãã使ç¨ãã¾ã // ãã¼ã¿ãåé¤ããå ´åã¯DELETEãªã¯ã¨ã¹ãã使ç¨ãã¾ã ); // mutateé¢æ°ã使ç¨ãã¦ãAPIãå¼ã³åºãã¾ã mutate({ id: 1234, title: 'test' }); // ããã§ãå®éã®ãã¼ã¿ã渡ãã¾ã
ä¸è¨ã®ã³ã¼ãã¯ã以ä¸ã®ãã¨ãè¡ã£ã¦ãã¾ãã
useMutationããã¯ã使ç¨ãã¦ãAPIå¼ã³åºããå®è¡ããããã®æºåããã¾ãã
ããã¦mutateé¢æ°ã使ç¨ãã¦ãAPIãå¼ã³åºãã¾ãããã¼ã¿ã渡ããã¨ã§ãAPIã«å¿ è¦ãªæ å ±ãæä¾ãã¾ãã
APIå¼ã³åºãã®çµæãããisLoadingãããisErrorãããisSuccessãããerrorãããdataããªã©ã®å¤æ°ã§åãåãã¾ãã
ãisLoadingãããisErrorãããisSuccessãããerrorãããdataãã®åå¤æ°ã¯ã以ä¸ã®ãããªå½¹å²ãæã£ã¦ãã¾ãã
ã» isLoading: APIå¼ã³åºããé²è¡ä¸ã§ãããã©ããã示ããã¼ã«å¤ã§ãã
ã» isError: APIå¼ã³åºãä¸ã«ã¨ã©ã¼ãçºçãããã©ããã示ããã¼ã«å¤ã§ãã
ã» isSuccess: APIå¼ã³åºããæåãããã©ããã示ããã¼ã«å¤ã§ãã
ã» error: APIå¼ã³åºãä¸ã«çºçããã¨ã©ã¼ã«é¢ããæ å ±ãå«ããªãã¸ã§ã¯ãã§ãã
ã» data: APIå¼ã³åºãã®çµæã¨ãã¦åå¾ãããã¼ã¿ãå«ããªãã¸ã§ã¯ãã§ãã
ã¯ã¨ãªã®ç¡å¹åã¨åæ¢
enabled
ãªãã·ã§ã³ãfalse
è¨å®ãããã¨ã§ä»¥ä¸ã®ããã«ãªãã¾ãã
ã» ãã£ãã·ã¥ãã¼ã¿ãåå¨ããå ´åã¯ãisSuccess
ç¶æ
ã«ãªãããã¼ã¿ãæä¾ããã¾ãã
ã» ãã£ãã·ã¥ãã¼ã¿ããªãå ´åã¯isIdle
ç¶æ
ã«ãªãããã¦ã³ãæã«ã¯ã¯ã¨ãªãå®è¡ããã¾ããã
ã» ããã¯ã°ã©ã¦ã³ãã§ã¯åã¯ã¨ãªããã¾ããã
ã» invalidateQueries
ããã³refetchQueries
ãçºç«ãããå ´åã§ããåã¯ã¨ãªãã¾ããã
ã» refetch
ã使ã£ã¦æåã§ã¯ã¨ãªãå®è¡ãããã¨ã¯å¯è½ã§ãã
enabledãªãã·ã§ã³ã¯ãã¯ã¨ãªã®å®è¡çµæã使ã£ã¦å¥ã®ã¯ã¨ãªãå®è¡ããéã«ä½¿ç¨ãã¾ãã
ä¸è¨ã®ä¾ã§ã¯ããuserIdããåå¨ããå ´åã«ã®ã¿ãã®ã¯ã¨ãªãå®è¡ãããããã«è¨å®ããã¦ãã¾ãã
const { isIdle, data: projects } = useQuery( ['projects', userId], getProjectsByUser, { // userIDãåå¨ããå ´åã®ã¿ãã®ã¯ã¨ãªãå®è¡ãã enabled: !!userId, }, );
ãenabledããªãã·ã§ã³ãfalse
ã«è¨å®ããã¦ããå ´åãã¯ã¨ãªã¯æåã§åå®è¡ããå¿
è¦ãããã¾ãã
invalidateQueries
ããã³refetchQueries
ã使ç¨ãã¦ããã¯ã¨ãªã¯åå®è¡ããã¾ããã
ã¾ãããenabledããªãã·ã§ã³ãtrue
ã«è¨å®ããã¨ãã¯ã¨ãªãèªåçã«åå®è¡ããã¾ãã
æå¾ã«
React-Queryã¯ãWebã¢ããªã±ã¼ã·ã§ã³ã§ãã¼ã¿ã管çããããã®ã©ã¤ãã©ãªã§ãã
ä¾ãã°ãAPIãããã¼ã¿ãåå¾ããéã«ããã¼ã¿ã®åå¾ç¶æ ã管çããå¿ è¦ã«å¿ãã¦ååå¾ãããã¨ãã§ãã¾ãã
ããã¾ã§ãReactã¢ããªã±ã¼ã·ã§ã³ã§ãã¼ã¿ç®¡çãè¡ãããã«ãReduxãããContext APIãã使ããã¦ãã¾ããããReact-Queryã使ããã¨ã§ãããç°¡åã«ãã¼ã¿ç®¡çãã§ããããã«ãªãã¾ããã
React-Queryã使ããã¨ã§ãAPIãããã¼ã¿ãåå¾ããå¦çãã«ã¹ã¿ã ããã¯ã«åé¢ãããã¨ãã§ãã¾ãã
ããã«ãããã³ã³ãã¼ãã³ãå ã§ãã¼ã¿åå¾ã«é¢ãããã¸ãã¯ãè¨è¿°ããå¿ è¦ããªããªããã³ã¼ãã®åå©ç¨æ§ãä¿å®æ§ãåä¸ãã¾ãã
ã¾ããReact-Queryã¯ããã¼ã¿ã®åå¾ç¶æ ãã¨ã©ã¼ã®ç®¡çããã£ãã·ã¥æ©è½ãªã©ãåãã¦ããããããã¼ã¿ã®åå¾ã«é¢ãããã¸ãã¯ãèªåã§å®è£ ããå¿ è¦ãããã¾ããã
ãã®ãããã³ã¼ãã®éãåæ¸ãããéçºå¹çãåä¸ãã¾ãã
React-Queryã¯ãReactã¢ããªã±ã¼ã·ã§ã³ã§ãã¼ã¿ç®¡çãè¡ãããã«ãé常ã«ä¾¿å©ãªã©ã¤ãã©ãªã§ãã
åå¦è ã§ãæ¯è¼çç°¡åã«å¦ã¶ãã¨ãã§ãããããä»å¾ã®Reactã¢ããªã±ã¼ã·ã§ã³éçºã«ããã¦ãæ´»ç¨ãããã¨ããå§ããã¾ãã
æ¬æ¥ã¯ä»¥ä¸ã¨ãªãã¾ãã
æå¾ã¾ã§èªãã§é ããããã¨ããããã¾ãã