https://github.com/ssi02014/next-tutorial-redux-scss
๐Next.js๋ก Redux(Redux-Saga), Scss Setting
https://github.com/ssi02014/next-tutorial-redux-scss
javacript nextjs react scss
Last synced: 8 months ago
JSON representation
๐Next.js๋ก Redux(Redux-Saga), Scss Setting
- Host: GitHub
- URL: https://github.com/ssi02014/next-tutorial-redux-scss
- Owner: ssi02014
- Created: 2021-04-20T06:58:05.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-20T10:42:42.000Z (over 4 years ago)
- Last Synced: 2024-12-30T06:45:07.646Z (9 months ago)
- Topics: javacript, nextjs, react, scss
- Language: JavaScript
- Homepage:
- Size: 73.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ป Next-Tutorial-Redux-Scss
### Next.js์ Redux-Saga์ Sass ์ ์ฉ
## ๐ฅ ์ฐธ๊ณ YouTube
### ๐ https://www.youtube.com/watch?v=UXMGGI3TSs4&t=622s
## ๐จ๐ปโ๐ป Install
- yarn add @zeit/next-sass node-sass@4.14.1
- yarn add next-redux-saga next-redux-wrapper react-redux redux redux-devtools-extension redux-saga
- ๋ค์ผ๋ก yarn add redux-thunk
## ๐จ๐ปโ๐ป Sass
- next.js์ sass๋ฅผ ์ ์ฉํ๋ ค๋ฉด @zeit/next-sass ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐ๋ก Installํด์ผ ๋๋ฉฐ, .next.config.js๋ฅผ ์์ฑํด์ ๋ค์ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผ ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ _app.js์ import "../scss/main.scss"; ๋ฅผ ํ๋ฉด ๋๋ค.
### ๐ .next.config.js
```js
const withSass = require("@zeit/next-sass");module.exports = withSass({
cssModules: true,
});
```
## ๐จ๐ปโ๐ป Redux-Saga
### ๐ store.js
- composeWithDevTools: redux devtools๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๋ฉ์๋
- createSagaMiddleware: redux-saga ์์ฑ
- createStore: Store ์์ฑ
- applyMiddleware: ๋ฏธ๋ค์จ์ด ์ ์ฉ
```js
import { createStore, compose, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import createSagaMiddleware from "redux-saga";import rootReducer from "./reducers";
import rootSaga from "./sagas";//redux-saga ์์ฑ
const sagaMiddleware = createSagaMiddleware();//์ด๊ธฐ initailState
const initialState = {};//๋ฏธ๋ค์จ์ด ์ฐ๊ฒฐ
const middleware = [sagaMiddleware];//๊ฐ๋ฐ ๋ชจ๋๋ผ๋ฉด composeWithDevTools
//๋ฐฐํฌ ๋ชจ๋๋ผ๋ฉด compose
const enhancer =
process.env.NODE_ENV === "production"
? compose(applyMiddleware(...middleware))
: composeWithDevTools(applyMiddleware(...middleware));//store ์์ฑ
const store = createStore(rootReducer, initialState, enhancer);//store์ rootSaga๋ฅผ ๋ฃ์ sagaMiddleware๋ฅผ ์คํ
store.sagaTask = sagaMiddleware.run(rootSaga);export default store;
```### ๐ _app.js
- Component๋ฅผ Provider๋ก ๋ฌถ์ด์ฃผ๋ฉด ๋ชจ๋ ์ปดํฌ๋ํธ์์ redux store๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
- createWrapper: getServerSideProps, getStaticProps ๊ฐ์ next์ ๋ผ์ดํ ์ฌ์ดํด์ redux๋ฅผ ๊ฒฐํฉ์ํค๋ ์ญํ . ๋ฐ๋ผ์, wrapper.withRedux๋ก ํ์ด์ง๋ฅผ ๊ฐ์ธ๊ฒ ๋๋ฉด redux๊ฐ ๊ฒฐํฉ๋ ๋ผ์ดํ์ฌ์ดํด์ ์ฌ์ฉํ ์ ์์
```js
import "../styles/globals.css";
import React from "react";
import { Provider } from "react-redux";
import { createWrapper } from "next-redux-wrapper";
import store from "../store/store";
import "../scss/main.scss";function MyApp({ Component, pageProps }) {
return (
;
);
}const makestore = () => store;
const wrapper = createWrapper(makestore);export default wrapper.withRedux(MyApp);
```
### ๐ sagas/index.js
- all: ์ ๋๋ ์ดํฐ ํจ์๋ฅผ ๋ฐฐ์ด์ ํํ๋ก ์ธ์๋ก ๋ฃ์ด์ฃผ๋ฉด, ์ ๋๋ ์ดํฐ ํจ์๋ค์ด ๋ณํ์ ์ผ๋ก ๋์์ ์คํ๋๊ณ , ์ ๋ถ resolve๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
- call: ํจ์์ ๋๊ธฐ์ ์ธ ํธ์ถ์ ํ ๋ ์ฌ์ฉ
- fork: ํจ์์ ๋น๋๊ธฐ์ ์ธ ํธ์ถ์ ํ ๋ ์ฌ์ฉ(์ฆ, call๊ณผ ๋ค๋ฅด๊ฒ ์์ ์๊ด์์ด ์คํํด์ผ๋ ๋ ์ฌ์ฉ)
```js
import { all, fork } from "redux-saga/effects";
import postSaga from "./postSaga";//์ ๋๋ ์ดํฐ
export default function* rootSaga() {
yield all([fork(postSaga)]);
}
```
### ๐ sagas/postSaga.js
- put: dispatch์ ๊ฐ์ ์ญํ ์ ํ๋ค.
- takeEvery: ๋ค์ด์ค๋ ๋ชจ๋ ์ก์ ์ ๋ํด ํน์ ์์ ์ ์ฒ๋ฆฌํ๋ค.
- takeLatest: ๊ธฐ์กด์ ์งํ ์ค์ด๋ ์์ ์ด ์๋ค๋ฉด ์ทจ์ํ๊ณ , ๊ฐ์ฅ ๋ง์ง๋ง์ผ๋ก ์คํ๋ ์์ ๋ง ์ํ
```js
import { GET_POSTS, GET_REQ } from "../types";
import { all, fork, put, takeEvery } from "redux-saga/effects";function* post() {
yield put({
type: GET_POSTS,
payload: ["1st post", "2nd posts", "3 posts"],
});
}function* watchPost() {
yield takeEvery(GET_REQ, post);
}//postSaga() ์ฌ๋ฌ Saga ํตํฉ
export default function* postSaga() {
yield all([fork(watchPost)]);
}```
### ๐ reducers/index.js
- combineReducers: ๋ง์ Reducer๋ค์ ํ๋๋ก ํฉ์ณ ํ๋์ Reducer๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ๋ง๋ค์ด์ค๋ค.
```js
import { combineReducers } from "redux";
import { postReducer } from "../reducers/postReducer";export default combineReducers({
post: postReducer,
});```
### ๐ reducers/postReducer.js
```js
import * as types from "../types";const initialState = {
posts: [],
post: {},
loading: false,
error: null,
};export const postReducer = (state = initialState, action) => {
switch (action.type) {
case types.GET_REQ:
return {
...state,
posts: [],
loading: true,
error: null,
};
case types.GET_POSTS:
return {
...state,
posts: action.payload,
loading: false,
error: null,
};
default:
return state;
}
};```