Skip to content

Commit

Permalink
Feat : redux toolkit 기본 세팅 #34
Browse files Browse the repository at this point in the history
- 문제 페이지에서 사용자가 선택하는 문제를 전역으로 관리하기 위한 redux toolkit 세팅
  • Loading branch information
sheepdog13 committed Feb 14, 2024
1 parent 6ee43f4 commit 6bac4cd
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 19 deletions.
6 changes: 4 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"proxy": "https://api.doit-toeic.xyz",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^2.2.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand All @@ -15,12 +16,13 @@
"@types/styled-components": "^5.1.34",
"axios": "^1.6.5",
"framer-motion": "^10.17.9",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.49.2",
"react-icons": "^4.12.0",
"react-redux": "^9.1.0",
"react-responsive": "^9.0.2",
"react-router-dom": "^6.21.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"react-spinners": "^0.13.8",
"styled-components": "^6.1.6",
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { Suspense, lazy } from 'react';
import { Provider } from 'react-redux';
import { store } from './store';
import ReactDOM from 'react-dom/client';
import './index.css';
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
Expand All @@ -13,9 +15,11 @@ const root = ReactDOM.createRoot(
);
root.render(
<Suspense fallback={<Loading />}>
<BrowserRouter>
<LazyApp />
</BrowserRouter>
<Provider store={store}>
<BrowserRouter>
<LazyApp />
</BrowserRouter>
</Provider>
</Suspense>,
);

Expand Down
18 changes: 18 additions & 0 deletions frontend/src/redux/_reducers/choices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createSlice } from '@reduxjs/toolkit';
import { Choice } from '../../types/Choice';

interface DataState {
choices: Choice[];
}

const initialState: DataState = {
choices: [],
};

export const choicesSlice = createSlice({
name: 'choices',
initialState,
reducers: {},
});

export default choicesSlice.reducer;
10 changes: 10 additions & 0 deletions frontend/src/redux/_reducers/rootReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { combineReducers } from '@reduxjs/toolkit';
import choices from './choices';

// reducer를 모아두는 rootreducer
const reducer = combineReducers({
choices,
});

export type ReducerType = ReturnType<typeof reducer>;
export default reducer;
6 changes: 6 additions & 0 deletions frontend/src/redux/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from '../store';

// js처럼 사용하기 위한 커스텀 hook
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
12 changes: 12 additions & 0 deletions frontend/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// src/redux/store.ts
import { configureStore } from '@reduxjs/toolkit';
import reducer from './redux/_reducers/rootReducer';

export const store = configureStore({
reducer,
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
4 changes: 4 additions & 0 deletions frontend/src/types/Choice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Choice = {
questionIndex: number;
answer: string;
};
133 changes: 119 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6bac4cd

Please sign in to comment.