03_lesson: dispatching fetchPosts async thunk

This commit is contained in:
Jason Zhu 2023-03-07 17:45:03 +11:00
parent f0d02e6e02
commit 2339529dfd
2 changed files with 19 additions and 2 deletions

View File

@ -1,11 +1,26 @@
import { useSelector } from 'react-redux';
import { selectAllPosts } from './postsSlice';
import { useDispatch, useSelector } from 'react-redux';
import {
fetchPosts,
getPostsError,
getPostsStatus,
selectAllPosts,
} from './postsSlice';
import PostAuthor from './PostAuthor';
import TimeAgo from './TimeAgo';
import ReactionButtons from './ReactionButton';
const PostsList = () => {
const dispatch = useDispatch();
const posts = useSelector(selectAllPosts);
const postsStatus = useSelector(getPostsStatus);
const postsError = useSelector(getPostsError);
useEffect(() => {
if (postsStatus === 'idle') {
dispatch(fetchPosts());
}
}, [postsStatus, dispatch]);
const orderedPosts = posts
.slice()

View File

@ -87,6 +87,8 @@ const postsSlice = createSlice({
});
export const selectAllPosts = (state) => state.posts.posts; // first posts is meaning name of the slice
export const getPostsStatus = (state) => state.posts.status;
export const getPostsError = (state) => state.posts.error;
export const { postAdded, reactionAdded } = postsSlice.actions;