728x90
반응형

redux-thunk는 기존 reduxcreateStoreapplyMiddleware를 추가하여  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 { createStoreapplyMiddleware } from 'redux';

 

import thunk from 'redux-thunk';

 

import App from './components/App';

import reducers from './reducers';

 

const store createStore(reducersapplyMiddleware(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

 

728x90
반응형

+ Recent posts

Powered by Tistory, Designed by wallel