IT_Web/React_Practice_Project

React actions/index.js] React, redux-thunk 라이브러리 활용하여 Blog 만들기

tantangerine 2020. 2. 25. 14:22
728x90
반응형

 

actions/index.js

import _ from 'lodash';

import jsonPlaceholder from '../apis/jsonPlaceholder';

 

export const fetchPostsAndUsers = () => async (dispatch, getState) => {

  await dispatch(fetchPosts());

 

  _.chain(getState().posts)

    .map('userId')

    .uniq()

    .forEach(id => dispatch(fetchUser(id)))

    .value();

};

 

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 });

};

 

 

 

728x90
반응형