๋ณดํต ๋ฆฌ๋์ค์์ ๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ์ฉํ๋ ์ฃผ๋ ์ฌ์ฉ ์ฉ๋๋ ๋น๋๊ธฐ ์์ ์ ์ฒ๋ฆฌํ ๋์ด๋ค. (redux-thunk, redux-sage ๋ฑ)
$ yarn add redux react-redux
2. ๋ฏธ๋ค์จ์ด ๋ง๋ค์ด๋ณด๊ณ ์ดํดํ๊ธฐ
๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด์ ํ ํ๋ฆฟ
const middleware = store => next => action => {
// ํ๊ณ ์ถ์ ์์
...
}
function middleware(store) {
return function (next) {
return function (action) {
// ํ๊ณ ์ถ์ ์์
...
};
};
};
store: ๋ฆฌ๋์ค ์คํ ์ด
next: ์ก์ ์ ๋ค์ ๋ฏธ๋ค์จ์ด์๊ฒ ์ ๋ฌํ๋ ํจ์ next(action)
action: ํ์ฌ ์ฒ๋ฆฌํ๊ณ ์๋ ์ก์ ๊ฐ์ฒด
๋ฏธ๋ค์จ์ด ์ง์ ์์ฑํด๋ณด๊ธฐ
<middlewares/myLogger.js>
const myLogger = store => next => action => {
console.log(action); // ๋จผ์ ์ก์
์ ์ถ๋ ฅํฉ๋๋ค.
const result = next(action); // ๋ค์ ๋ฏธ๋ค์จ์ด (๋๋ ๋ฆฌ๋์) ์๊ฒ ์ก์
์ ์ ๋ฌํฉ๋๋ค.
// ์
๋ฐ์ดํธ ์ดํ์ ์ํ๋ฅผ ์กฐํํฉ๋๋ค.
console.log('\t', store.getState()); // '\t' ๋ ํญ ๋ฌธ์ ์
๋๋ค.
return result; // ์ฌ๊ธฐ์ ๋ฐํํ๋ ๊ฐ์ dispatch(action)์ ๊ฒฐ๊ณผ๋ฌผ์ด ๋ฉ๋๋ค. ๊ธฐ๋ณธ: undefined
};
export default myLogger;
๋ฏธ๋ค์จ์ด ์ ์ฉํ๊ธฐ
import { applyMiddleware } from 'redux';
<index.js>
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from './modules';
import myLogger from './middlewares/myLogger';
const store = createStore(rootReducer, applyMiddleware(myLogger));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
serviceWorker.unregister();
3. redux-logger ์ฌ์ฉ
$ yarn add redux-logger
<index.js>
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from './modules';
import logger from 'redux-logger';
const store = createStore(rootReducer, applyMiddleware(logger)); // ์ฌ๋ฌ๊ฐ์ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉ ํ ์ ์์ต๋๋ค.
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
serviceWorker.unregister();
Redux DevTools ์ฌ์ฉํ๊ธฐ
$ yarn add redux-devtools-extension
import { composeWithDevTools } from 'redux-devtools-extension';
<index.js>
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from './modules';
import logger from 'redux-logger';
import { composeWithDevTools } from 'redux-devtools-extension';
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(logger))
); // ์ฌ๋ฌ๊ฐ์ ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉ ํ ์ ์์ต๋๋ค.
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
serviceWorker.unregister();
8. json-server => ๊ฐ์ง ์ฐ์ต์ฉ ์๋ฒ ๋ง๋ค๊ธฐ!
https://react.vlpt.us/redux-middleware/08-json-server.html
9. CORS์ WebpackDevServer Proxy
=> ๋ธ๋ผ์ฐ์ ์ ํ์ฌ ์ฃผ์์ API ์ฃผ์์ ๋๋ฉ์ธ์ด ์ผ์นํ์ง ์์ ๋ CORS ์ค์ ํ ํ์์์
๋ธ๋ผ์ฐ์ API๋ฅผ ์์ฒญํ ๋ ๋ฐฑ์๋ ์๋ฒ์ ์ง์ ์ ์ผ๋ก ์์ฒญํ์ง ์๊ณ , ํ์ฌ ๊ฐ๋ฐ ์๋ฒ์ ์ฃผ์๋ก ์์ฒญํ๋ค.
https://react.vlpt.us/redux-middleware/09-cors-and-proxy.html
'โ๏ธ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ๊ณต๋ถํ๊ธฐ(7) - redux-saga of ๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด (0) | 2021.11.09 |
---|---|
React ๊ณต๋ถํ๊ธฐ(7) - redux-thunk of ๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด (0) | 2021.11.09 |
React ๊ณต๋ถํ๊ธฐ(6) - ๋ฆฌ๋์ค(Redux) (0) | 2021.11.05 |
React ๊ณต๋ถํ๊ธฐ(5) - ๋ฆฌ์กํธ ๋ผ์ฐํฐ (0) | 2021.11.05 |
React ๊ณต๋ถํ๊ธฐ(4) - API ์ฐ๋ํ๊ธฐ (0) | 2021.11.05 |