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
반응형
'IT_Web > React_Practice_Project' 카테고리의 다른 글
React redux-thunk 구현 Blog만들기 총파일 (0) | 2020.03.09 |
---|---|
React components/SongList.js] React, react-redux 활용 연습 (0) | 2020.02.25 |