redux-thunk는 기존 redux의 createStore에 applyMiddleware를 추가하여 action함수에서 dispatch를 하게 해 준다
그 결과 밑에 로직처럼 axios를 사용하여 결과 값을 dispatch 한다
export const fetchPosts = () => async dispatch => {
const response = await jsonPlaceholder.get('/posts');
dispatch({ type: 'FETCH_POSTS', payload: response.data });
};
export const fetchUser = id => async dispatch => {
const response = await jsonPlaceholder.get(`/users/${id}`);
dispatch({ type: 'FETCH_USER', payload: response.data });
};
Provider의 property 값에 applyMiddleware 추가하여 선언한다
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import App from './components/App';
import reducers from './reducers';
const store = createStore(reducers, applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector('#root')
);
그 이외 연결 및 사용방법은 기존 redux활용과 같다
밑의 url을 참고하자
https://tantangerine.tistory.com/3
react-redux 연결하기, reducer 활용하기
react와 redux를 연결하는 방법을 간략하게 알아보고 이때 사용되는 react-reudx와 reducer의 관계를 알아보자 * 일단 변수명은 지금 로직과 관련이 없다는 것만 알고 다양한 연결방법과 쓰임새만 알고가자 reuduc..
tantangerine.tistory.com
'IT_Web > React' 카테고리의 다른 글
Reducer _ Lodash활용 데이터 다루기 (0) | 2020.04.01 |
---|---|
React 이벤트 처리 종류 (0) | 2020.03.09 |
React, Hooks로 react-redux(connect방법 및 사용방법) (0) | 2020.03.09 |
React PublicRoute & PrivateRoute 활용법 (0) | 2020.02.25 |
react-redux 연결하기, reducer 활용하기 (0) | 2020.02.25 |