Skip to content

light9639/RTK-Query-TypeScript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช Redux-Toolkit-Query๋ฅผ ์ด์šฉํ•œ ์˜ˆ์ œ ํŒŒ์ผ.

:octocat: ๋ฐ”๋กœ ๊ฐ€๊ธฐ : https://light9639.github.io/RTK-Query-TypeScript/

light9639 github io_RTK-Query-TypeScript_

โœจ ๐Ÿช Redux-Toolkit-Query๋ฅผ ์ด์šฉํ•œ ์˜ˆ์ œ ํŒŒ์ผ. โœจ

๐ŸŽ‰ React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  • React ์ƒ์„ฑ
npm create-react-app my-app
# or
yarn create react-app my-app
  • vite๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด
npm create vite@latest
# or
yarn create vite
  • ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ ํ›„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ๋งŒ๋“  ํ›„ React ์„ ํƒ, Typescirpt-SWC ์„ ํƒํ•˜๋ฉด ์ƒ์„ฑ ์™„๋ฃŒ.

๐ŸŒŸ Redux-Toolkit ์„ค์น˜

  • Redux-Toolkit ์„ค์น˜ ๋ช…๋ น์–ด
npm install redux react-redux @reduxjs/toolkit
# or
yarn add redux react-redux @reduxjs/toolkit

โœ’๏ธ App.tsx, main.tsx, Pokemon.tsx, pokemon.ts ์ˆ˜์ • ๋ฐ ์ž‘์„ฑ

โšก App.tsx

  • map ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ค‘ ์ผ๋ถ€๋ฅผ ๊ฐ€์ ธ์™€ ํ™”๋ฉด์— ์ƒ์„ฑํ•œ๋‹ค.
  • select์˜ onChange ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช‡์ดˆ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ์ง€๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
import Pokemon from './Pokemon'

const pokemon = ['bulbasaur', 'pikachu', 'ditto', 'bulbasaur']

export default function App() {
  const [pollingInterval, setPollingInterval] = useState<number>(0)

  return (
    <div className="App">
      <div>
        <a href="https://ko.redux.js.org/introduction/getting-started/" target="_blank" rel="noreferrer">
          <img src="https://camo.githubusercontent.com/7b7f04b16cc2d2d4a32985710e4d640985337a32bbb1e60cdacede2c8a4ae57b/68747470733a2f2f63646e2e776f726c64766563746f726c6f676f2e636f6d2f6c6f676f732f72656475782e737667" className="logo" alt="Vite logo" />
        </a>
        <a href="https://reactjs.org" target="_blank" rel="noreferrer">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Redux-Toolkit-Query</h1>
      <select
        onChange={(change) => setPollingInterval(Number(change.target.value))}
      >
        <option value={0}>Off</option>
        <option value={1000}>1s</option>
        <option value={5000}>5s</option>
      </select>
      <div className='Content_Wrapper'>
        {pokemon.map((poke, index) => (
          <Pokemon key={index} name={poke} pollingInterval={pollingInterval} />
        ))}
      </div>
    </div>
  )
}

โšก main.tsx

  • ApiProvider๋Š” ๋ฆฌ๋•์Šค์ฟผ๋ฆฌ์—์„œ pokemonApi๋Š” pokemon.ts์—์„œ import ํ•œ ๋‹ค์Œ <App />์„ ๊ฐ์‹ธ๋ฉด ๋ฆฌ๋•์Šค ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'
import { ApiProvider } from '@reduxjs/toolkit/query/react'
import { pokemonApi } from './services/pokemon'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <React.StrictMode>
    <ApiProvider api={pokemonApi}>
      <App />
    </ApiProvider>
  </React.StrictMode>,
)

โšก Pokemon.tsx

  • useGetPokemonByNameQuery๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋”ฉ์ค‘์ผ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„ ๋•Œ ํ™”๋ฉด์„ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
import { useGetPokemonByNameQuery } from './services/pokemon'
import React from 'react'

interface PokeType {
  name: string
  pollingInterval: number
}

export default function Pokemon({ name, pollingInterval }: PokeType): JSX.Element {
  const { data, error, isLoading, isFetching } = useGetPokemonByNameQuery(name, { pollingInterval })

  if (error) return (<>Oh no, there was an error</>)

  if (isLoading) return (<>Loading...</>)

  return (
    <div className='Content_Box'>
      <h3>
        {data.species.name} {isFetching ? '...' : ''}
      </h3>
      <img src={data.sprites.front_shiny} alt={data.species.name} />
    </div>
  )
}

โšก pokemon.ts

  • createApi, fetchBaseQuery๋ฅผ import ํ•œ ๋‹ค์Œ pokemonApi๋ฅผ useGetPokemonByNameQuery๋กœ ๋‚ด๋ณด๋‚ด ์‚ฌ์šฉํ•œ๋‹ค.
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

export const pokemonApi = createApi({
  reducerPath: 'pokemonApi',
  baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
  tagTypes: [],
  endpoints: (builder) => ({
    getPokemonByName: builder.query({
      query: (name: string) => `pokemon/${name}`,
    }),
  }),
})

// ์‚ฌ์šฉํ•  hook๋ฅผ ๋‚ด๋ณด๋‚ด๊ธฐ
export const { useGetPokemonByNameQuery } = pokemonApi

About

๐Ÿช Redux-Toolkit-Query๋ฅผ ์ด์šฉํ•œ ์˜ˆ์ œ ํŒŒ์ผ.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published