React

React-query ์ƒํƒœ ๊ด€๋ฆฌ

martinooo 2022. 11. 26. 11:17

๐Ÿ“–React-query ์•Œ๊ธฐ์ „์— JSX๋ž€?


  • JSX(javascript XML)๋Š” javascript์— XML์„ ์ถ”๊ฐ€ํ•œ ํ™•์žฅํ•œ ๋ฌธ๋ฒ•์ด๋‹ค.
  • JSX๋Š” React์—์„œ ์‚ฌ์šฉ๋˜๋Š”๋ฐ ๊ณต์‹์ ์ธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์€ ์•„๋‹ˆ๋‹ค.
  • JSX๋Š” javascript ํ™•์žฅํ•œ ๋ฌธ๋ฒ•์œผ๋กœ ๋ฐ”๋ฒจ๋“ฑ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋žœ์ŠคํŒŒ์ผ๋Ÿฌ๋กœ javascript ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜์ด ๋œ๋‹ค.
  • JSX ๋ฌธ๋ฒ•์€ ๋ฐ˜๋“œ์‹œ ๋ถ€๋ชจ์š”์†Œ ํ•˜๋‚˜๊ฐ€ ๊ฐ์‹ธ๋Š” ํ˜•ํƒœ ์—ฌ์•ผํ•œ๋‹ค.
    • <> </> ๋ฌธ๋ฒ•์œผ๋กœ ๋ถ€๋ชจ์š”์†Œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. 
  • JSX์—์„œ javascript ํ‘œํ˜„์‹์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด {}๋กœ ์ฝ”๋“œ๋ฅผ ๊ฐ์‹ธ์ค˜์•ผํ•œ๋‹ค.
    • {isLoading && <div>Loading</div>} 
  • JSX์—์„œ class์‚ฌ์šฉ์ด ์•„๋‹Œ className์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐ŸคณReact-query ๋ž€?

 

 


  • React-query๋Š” ์„œ๋ฒ„ ์ƒํƒœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์˜ ๋น„๋™๊ธฐ์ ์ธ ๋™์ž‘์„ ๊ด€๋ฆฌํ•˜๊ธฐ์— ์ ํ•ฉํ•˜๋‹ค!
  • React application์—์„œ ์„œ๋ฒ„ ์ƒํƒœ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ , ์บ์‹ฑํ•˜๋ฉฐ, ์ง€์†์ ์œผ๋กœ ๋™๊ธฐํ™”ํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ž‘์—…์„ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋‹ค.
  • React-query๋ฅผ ์‚ฌ์šฉํ•ด์„œ axios, fetch๋“ฑ์™€ ๊ฐ™์ด ๋น„๋™๊ธฐ ์š”์ฒญ์„ ๋ณด๋‚ด์ฃผ๋Š” ๋„๊ตฌ๋“ค๊ณผ ํ•จ๊ป˜ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉ์„ ํ•  ์ˆ˜์žˆ๋‹ค.

๐Ÿคณwhy? React-query!


  • ์„œ๋ฒ„๋ฐ์ดํ„ฐ์˜ ์บ์‹œ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. ์ฆ‰ React-query ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์บ์‹œ๋ฅผ ์œ ์ง€ ๊ด€๋ฆฌํ•œ๋‹ค. 
    • ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š”๊ฒƒ์€ React-query์ด์ง€๋งŒ ์„œ๋ฒ„์˜ ์ƒˆ ๋ฐ์ดํ„ฐ๋กœ ์บ์‹œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ณ  ์„ค์ •ํ•˜๋Š”๊ฒƒ์€ ์‚ฌ์šฉ์ž์ด๋‹ค. 
    • React-query๋Š” ์„ค์ •์— ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ, ์บ์‹ฑ๊ด€๋ฆฌ๋“ฑ์„ ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • react-query๋Š” loading, error state ๊ด€๋ฆฌ, pagination & infinite scroll ๊ด€๋ฆฌ, prefetching, mutations ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ๊ด€๋ฆฌ, ์ค‘๋ณต์ œ๊ฑฐ ๋“ฑ๋“ฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•˜๊ณ  ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๐Ÿ‘€React-query ์‚ฌ์šฉ๋ฐฉ๋ฒ•


  • npm install react-query
  • query client ๋งŒ๋“ค์–ด์„œ query์™€ cache๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ•„์š” 
  • query provider ์‚ฌ์šฉํ•ด์„œ ์ „์—ญ์œผ๋กœ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋“ค์ด React-qeury๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒํ•จ
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient();

ReactDOM.render(
   <QueryClientProvider client={queryClient}>
      <App />
   </QueryClientProvider>
  document.getElementById('root')
);

React ์ „์—ญ์— React-query provider๋ฅผ ์ ์šฉํ•ด์ค€๋‹ค. 

 import { useQuery } from "react-query";
 
 const { data, isError, isLoading } = useQuery(
    'posts', 
    fetchPosts,
  );
  
  if(isLoading) return <h3>Loading...</h3>;
  if(isError) return <h3>Oops, something went wrong</h3>;

useQuery ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ fetching ํ•œ๋‹ค. 

useQuery ์•ˆ์— queryKey ๋ฐ์ดํ„ฐ๋ฅผ ํŒจ์นญํ• ๋•Œ key์ด๋ฉฐ ํ•„์ˆ˜์ ์œผ๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

"fetchPosts๋Š” axios ํ˜น์€ fetch์˜ ํ†ต์‹ ์„ ํ•จ์ˆ˜๋กœ ๋”ฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค."

async function fetchPosts() {
  const response = await fetch(
    `API ํ†ต์‹  URL`
  );
  return response.json();
}

๊ธฐ๋ณธ์ ์ธ React-qeury ์‚ฌ์šฉ๋ฒ•์ž…๋‹ˆ๋‹ค.