Redux + redux-thunk

Happheeยท2021๋…„ 12์›” 15์ผ
0

[ImagineCup] NVP

๋ชฉ๋ก ๋ณด๊ธฐ
2/8
post-thumbnail

๐Ÿ–ฅ React Native + Redux + redux-thunk

  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ App.js ์—์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌ state
  • store์—์„œ ์ด๋ฅผ ์†Œ์œ ํ•˜๊ณ  ๊ด€๋ฆฌ
  • action ๊ณผreducer์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚จ๋‹ค
  • axios์™€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•œ middleware๋กœ
    redux-thunk ๋ฅผ ์‚ฌ์šฉ

๐Ÿ“ ์‚ฌ์šฉ์ด์œ 

  • ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋ฅผ ํŽธํ•˜๊ฒŒ ํ•ด์•ผํ•  ๊ฒƒ๊ฐ™๋‹ค.
    ์™œ๋ƒํ•˜๋ฉด,,,,,
    ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ํ…๋ฐ, ์ •๋ณด๋“ค์ด ํฉ์–ด์ ธ์žˆ์œผ๋ฉด ์œ ์ง€/๋ณด์ˆ˜ํ•˜๊ธฐ ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™๊ณ ,
  • ๋ฐ์ดํ„ฐ์˜ ๋ณด์•ˆ์ด ์ค‘์š”ํ•œ ํ”„๋กœ์ ํŠธ์ด๊ธฐ์— ์ด๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
    Redux๋ฅผ ์„ ํƒํ–ˆ๋‹ค.

๐Ÿ“ redux ์„ค์น˜

npm i --save react-redux redux

๐Ÿ“ redux-thunk ์„ค์น˜

npm i redux-thunk

๐Ÿ–ฅ ํด๋” ๊ตฌ์กฐ

  • src

    • lib

      • axiosInstance.js
    • store

      • actions
        • actionTypes.js
        • user.js
        • login.js
      • reducers
        • auth.js
        • user.js
        • login.js
        • rootReducers.js
      • store.js
    • cotainers (๋ฆฌ๋“€์„œ ์ž‘๋™ํ•˜๋Š” ๊ณต๊ฐ„)

      • auth
        1. Login.js
        2. TakeTextMessage.js
        3. CheckCertificate.js
        4. TakeIdCard.js
        5. TakeVaccinePass.js
        6. SetPassword.js
        7. ResetPassword.js
        8. ResetTextMessage.js
    • components (view ๋‹ด๋‹น)

      • auth
        1. Login.js
        2. TakeTextMessage.js
        3. CheckCertificate.js
        4. TakeIdCard.js
        5. TakeVaccinePass.js
        6. SetPassword.js
        7. ResetPassword.js
        8. ResetTextMessage.js
    • screens (๊ตฌํ˜„ ์Šคํฌ๋ฆฐ)

      • AuthStack
        1. Login.js
        2. TakeTextMessage.js
        3. CheckCertificate.js
        4. TakeIdCard.js
        5. TakeVaccinePass.js
        6. SetPassword.js
        7. ResetPassword.js
        8. ResetTextMessage.js
      • HomeStack
        1. MyCretificate.js
      • SettingStack
        1. RegisterNVP.js (CheckCertificate์™€ ๊ฐ™์Œ)

๐Ÿ–ฅ store

redux๋กœ๋ถ€ํ„ฐ createStore, applyMiddlewareํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  redux-thunk๋กœ๋ถ€ํ„ฐ thunk๋ฅผ ๊ฐ€์ ธ์™€ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ ์šฉํ•œ ์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • store/store.js
import { createStore, applyMiddleware } from "redux";
import rootReducers from "./reducers/rootReducers";
import thunk from 'redux-thunk'

const store = createStore(rootReducers, applyMiddleware(thunk));

export default store;

๐Ÿ–ฅ actions

REST API

const USER_URL : "api/user"
  • ๊ฐ„ํŽธ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋กœ๊ทธ์ธ GET
  • ํ•ธ๋“œํฐ ๋ฌธ์ž ์ธ์ฆ GET, POST
  • ๋ฌธ์ž ์ธ์ฆ ์œ ํšจ์‹œ๊ฐ„
  • ์‹ ๋ถ„์ฆ ๋“ฑ๋ก POST
  • ๋ฐฑ์‹  ์ฆ๋ช…์„œ ๋“ฑ๋ก POST
  • ๊ฐ„ํŽธ ๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ • POST
  • ๊ฐ„ํŽธ ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌํ™•์ธ GET
  • ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ์‹œ ๋ฌธ์ž ์ธ์ฆ GET, POST
  • ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ •PUT

actionTypes.js

  • state๋งŒ! ๊ด€๋ จ์žˆ๋Š” action์€ type์ด ํ•˜๋‚˜์ด๋‹ค.
//password
export const SET_PASSWORD = "SET_PASSWORD";
export const RESET_PASSWORD = "RESET_PASSWORD";
//idCard
export const REGISTER_ID_CARD = "REGISTER_ID_CARD";
export const REGISTER_VACCINE_PASS = "REGISTER_VACCINE_PASS";
//re_password
export const CONFIRM_PASSWORD = "CONFIRM_PASSWORD";
export const MODIFY_PASSWORD = "MODIFY_PASSWORD";
  • api ํ†ต์‹ ๊ณผ ๊ด€๋ จ๋œ action์€ _SUCCESS, _FAILURE๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ redux-thunk๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
//login
export const LOGIN = "LOGIN";
export const LOGIN_SUCCESS = "LOGIN_SUCCESS";
export const LOGIN_FAILURE = "LOGIN_FAILURE";
//autoLogin
export const AUTO_LOGIN = "AUTO_LOGIN";
export const AUTO_LOGIN_SUCCESS = "AUTO_LOGIN_SUCCESS";
export const AUTO_LOGIN_FAILURE = "AUTO_LOGIN_FAILURE";

๐Ÿ–ฅ reducers

๋ชจ๋“  reducer๋ฅผ ๊ฐ€์ ธ์™€ combineReducers๋กœ ํ•œ๋ฒˆ์— root๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค

  • reducers/rootReducer.js
import { combineReducers } from "redux";
import loginReducer from "./login";
import authReducer from "./auth";
import userReducer from "./user";
const rootReducers = combineReducers({
    login: loginReducer
    auth: authReducer,
    user: userReducer,
});

export default rootReducers;
profile
์ฆ๊ธฐ๋ฉด์„œ ์ •ํ™•ํ•˜๊ฒŒ ๋‚˜์•„๊ฐ€๋Š” ์›นํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž https://happhee-dev.tistory.com/ ๋กœ ์ด์ „ํ•˜์˜€์Šต๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€