Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createSlice } from '@reduxjs/toolkit';
- export const postsSlice = createSlice({
- name: 'post',
- initialState: {
- lastID: 0,
- posts: [],
- authors: []
- },
- reducers: {
- createPost: (state, action) => {
- state.posts.push({
- id: ++state.lastID,
- author: action.payload.author,
- text: action.payload.text
- });
- if(!state.authors.includes(action.payload.author))
- state.authors.push(action.payload.author);
- }
- }
- });
- export const { createPost } = postsSlice.actions;
- export const selectPosts = (state) => state.post.posts;
- export const selectAuthors = (state) => state.post.authors;
- export const selectPostsByAuthor = postAuthor => state => {
- return state.post.posts.filter(({ author }) => postAuthor === author).length;
- };
- export default postsSlice.reducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement