在 react-redux 应用程序中,什么会使操作分派更新状态中没有此类操作的还原器中的状态

时间:2021-03-16 11:19:54

标签: javascript reactjs redux react-redux dispatch

我在 react-redux 应用程序中分派了一个动作,具有该动作的 reducer 的状态根据需要使用该动作的有效负载进行更新。除此之外,即使没有分派相应的动作,另一个没有这样动作的减速器的状态也会用相同的有效载荷更新。到底发生了什么?

//my action for reducer 1
import { CREATE, UPDATE, DELETE, FETCH_ALL, ITEMS_LOADING } from '../constants/actionTypes';
import * as api from '../api/index';

export const getPosts = () => async (dispatch) => {
    dispatch(setItemsLoading());
    const data = await api.fetchPosts
        .then((res) => {
            dispatch({ type: FETCH_ALL, payload: res.data });
        }).catch(() => {
            console.log("there is error");
        })

}
//my action for reducer 2
import { GET_POST } from '../constants/actionTypes';
import * as api from '../api/index';

export const getSinglePosts = (id) => (dispatch) => {
    return{
        type: GET_POST, payload: id
    }
}

//reducer 1
import { CREATE, UPDATE, DELETE, FETCH_ALL, ITEMS_LOADING } from '../constants/actionTypes';
const initialState = {
    posts: [],
    allLoaded:false,
    loading: false
}
export default (state = initialState, action) => {
    switch (action.type) {
        case DELETE:
            return {
                ...state,
                posts: state.posts.filter((post) => post._id !== action.payload)
            };
        case UPDATE:
            return {
                ...state,
                posts: state.posts.map((post) => post._id === action.payload._id ? action.payload : post)
            };
        case FETCH_ALL:
            return {
                ...state,
                posts: action.payload,
                allLoaded:true,
                loading: false
            };
        case CREATE:
            return {
                ...state,
                posts: [action.payload, ...state.posts]
            };
        case ITEMS_LOADING:
            return {
                ...state,
                loading: true
            };
        default:
            return state;
    }
}

//reducer2

import {
    GET_POST
} from '../constants/actionTypes';

const initialState = {
    info:null,
    postLoaded:false
}

export default function (state = initialState, action) {
    switch (action.type) {
        case GET_POST:
            return {
                ...state,
                postLoaded: true,
                info:action.payload
            };
        default:
            return state;
    }
}

//homepage where action was dispatched
import React, { useState, useEffect } from 'react';
import PageNavbar from './PageNavbar';
import { getPosts } from '../../myActions/posts';
import { useSelector, useDispatch } from 'react-redux';
//import { getSinglePosts } from '../../myActions/single';
import { useHistory } from 'react-router-dom';
import Button from '@material-ui/core/Button';

function MyBlog() {
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getPosts());
  }, [dispatch]);
  const posts = useSelector((state) => state.posts.posts);
  const history = useHistory();
  return (
    <>
      <PageNavbar />
      <div align="center">
        {posts.map((post) => (
          <>
            <Button href="/user/singlePost" color="primary">
              {post.title}
            </Button>
            <br />
          </>))}
      </div>
    </>
  )
}

export default MyBlog
//root reducer
import { combineReducers } from 'redux';
import posts from './myposts';
import error from './error';
import auth from './auth';
import info from './singlePost';

export default combineReducers({
    info,
    posts,
    auth,
    error
})
//thanks

0 个答案:

没有答案