Code Monkey home page Code Monkey logo

redux-toolkit-axios-asyncthunk's Introduction

๐Ÿš€ Redux-Toolkit์˜ AsyncThunk ๊ธฐ๋Šฅ์„ Axios๋กœ ๊ตฌํ˜„ํ•œ ์—ฐ์Šต ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.

:octocat: https://light9639.github.io/Redux-Toolkit-Axios-AsyncThunk/

light9639 github io_Redux-Toolkit-Axios-AsyncThunk_

โœจ Redux-Toolkit์˜ AsyncThunk ๊ธฐ๋Šฅ์„ Axios๋กœ ๊ตฌํ˜„ํ•œ ์—ฐ์Šต ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. โœจ

๐ŸŽ‰ 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 ์„ ํƒํ•˜๋ฉด ์ƒ์„ฑ ์™„๋ฃŒ.

๐Ÿ›ฉ๏ธ Redux-Toolkit, Axios ์„ค์น˜

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

โœ’๏ธ main.tsx, App.tsx, userSlice.ts, store.ts, useTypedSelector.ts, TypeBox.ts ์ˆ˜์ • ๋ฐ์ž‘์„ฑ

โšก main.tsx

  • react-redux์—์„œ Provider ํ•จ์ˆ˜ ๊ฐ€์ ธ์˜จ ํ›„ store.ts ํŒŒ์ผ์„ import ํ•œ ํ›„ <Provider store={store}></Provider>์œผ๋กœ <App />์„ ๋‘˜๋Ÿฌ์‹ธ๋ฉด Redux-Toolkit ์‚ฌ์šฉ์ค€๋น„ ์™„๋ฃŒ.
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import { Provider } from 'react-redux'
import { store } from './redux/store'

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

โšก App.tsx

  • redux ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด useSelector, useDispatch๋ฅผ import ํ•œ ๋’ค์— ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์œ„์˜ 2๊ฐ€์ง€ ํ•จ์ˆ˜๋“ค์€ type ์ ์šฉ์ด ์•ˆ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— useTypedSelector.ts์˜ useAppDispatch, useAppSelector๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค.
import { useSelector, useDispatch } from "react-redux";
import { fetchUser } from "./redux/userSlice";
import React, { useEffect } from "react";
import { RootState } from "./redux/store";
import { Dispatch } from "redux";
import { useAppDispatch, useAppSelector } from "./hooks/useTypedSelector";

export default function App(): JSX.Element {
  const dispatch = useAppDispatch();
  // RootState๊ฐ€ useSelector์—์„œ state์˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ๋œ ๊ฒƒ
  const { users, loading, error } = useAppSelector(state => state.users);

  return (
    <div className="App" style={{ margin: "0 auto", textAlign: "center" }}>
      <h1>Redux-tookit with Thunk</h1>
      <button onClick={() => dispatch(fetchUser())} style={{ display: "inline-block", padding: "10px 15px", borderRadius: "5px" }}>์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ</button>
      {users?.length > 0 &&
        users.map((user) =>
          <div key={user.index} style={{ textAlign: "center", margin: "0 auto" }}>
            <img src={user.src} alt={user.alt} style={{ display: "block", maxWidth: "300px", margin: "25px auto", borderRadius: "15px" }} />
            {user.name}
          </div>
        )}
    </div>
  )
}

โšก userSlice.ts

  • Redux-toolkit์˜ ๋‚ด์žฅ ๊ธฐ๋Šฅ์ธ createAsyncThunk๋ฅผ ์ƒ์„ฑ ํ›„ extraReducers ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด axios๋ฅผ ํ†ตํ•œ ์ž๋ฃŒ ์ „์†ก์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import axios from "axios";
import { store } from "./store";
import { CommonType } from "../Type/TypeBox";

interface initialType {
    users: CommonType[],
    loading: boolean,
    error: string | undefined
}

const initialState: initialType = {
    users: [],
    loading: false,
    error: ""
}

export const fetchUser = createAsyncThunk(
    'counterSlice/asyncUpFetch',
    async () => {
        return axios({
            method: "get",
            url: "https://raw.githubusercontent.com/light9639/Shoe-Store/main/data/Shoes.json"
        }).then(response => response.data.Kids);
    }
)

// slice ์ƒ์„ฑ
const usersSlice = createSlice({
    // slice ์ด๋ฆ„ ์ •์˜
    name: "users",
    // ์ดˆ๊ธฐ ๊ฐ’
    initialState,
    // ๋ฆฌ๋“€์„œ
    reducers: {},
    extraReducers: (builder) => {
        builder.addCase(fetchUser.pending, (state) => {
            state.loading = true;
        }),
            builder.addCase(fetchUser.fulfilled, (state, action) => {
                state.users = action.payload;
                state.loading = false;
            }),
            builder.addCase(fetchUser.rejected, (state, action: PayloadAction<any>) => {
                state.loading = false;
            })
    }
});

// useSelector ์‚ฌ์šฉ์‹œ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
export type RootState = ReturnType<typeof store.getState>
// useDispatch๋ฅผ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
export type AppDispatch = typeof store.dispatch

// slice๋ฅผ ๋‚ด๋ณด๋ƒ„
export default usersSlice.reducer;

โšก store.ts

  • configureStore์•ˆ์— userSlice์˜ reducer๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„ export ํ•จ์œผ๋กœ์จ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ.
import { configureStore } from "@reduxjs/toolkit";
import { useDispatch } from 'react-redux';
import userReducer from "./userSlice";

export const store = configureStore({
    reducer: {
        users: userReducer
    }
});

โšก useTypedSelector.ts

  • useDispatch, useSelector์˜ ํƒ€์ž…์€ ๋ฒˆ๋ฒˆํžˆ ์ง€์ •ํ•˜๊ธฐ๋ณด๋‹ค useAppDispatch, useAppSelector๋ฅผ ์ €์žฅํ•˜๊ณ  import ํ•จ์œผ๋กœ์จ type ์ง€์ •์˜ ์ˆ˜๊ณ ๋ฅผ ๋œ ์ˆ˜ ์žˆ๋‹ค.
import { useDispatch, useSelector } from "react-redux";
import type { TypedUseSelectorHook } from "react-redux";
import type { RootState, AppDispatch } from "../redux/store";

export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

โšก TypeBox.ts

  • axios๋กœ ๊ฐ€์ ธ์˜ฌ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž… ์ง€์ •.
export type CommonType = {
    index: number;
    src: string;
    alt: string;
    name: string;
    info: string;
    price: string;
    Gender: string;
    href: string;
    star: {
        first: string;
        second: string;
        third: string;
        four: string;
        five: string;
    };
    Review: number;
    count: number;
}

๐Ÿ“Ž ์ถœ์ฒ˜

redux-toolkit-axios-asyncthunk's People

Contributors

light9639 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

ckck803

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.