【问题标题】:Redux store isn't updatingRedux 商店没有更新
【发布时间】:2019-11-06 14:20:41
【问题描述】:

我正在尝试使用 groupsNames 列表更新 redux 存储。 动作内部的 axios 调用有效并且有响应。 当我分派有效负载时,redux 存储不会使用响应数据进行更新。

文件名:actions/index.js

代码:

export const getGroups = () => async dispatch => {
  axios.get(url + "groups")
   .then(response => {
     console.log(response.data);
    dispatch({
     type: GET_GROUPS,
      payload: response.data
   })
  },
  function(error) {
    console.log(error)
          }
      );

 };

文件名:reducers/groupsReducer.js

代码:

import _ from 'lodash';
import {
GET_GROUPS,
} from '../actions/types';


export default (state = {}, action) => {
console.log(action.payload);
console.log("inside groupsREducer");
switch (action.payload) {
case GET_GROUPS:
   console.log(action.payload);
   return {...state, ..._.mapKeys(action.payload, 'id')};

default:
  return state;
 }
 }

//mapKeys 是一个来自lowdadsh 的函数,它接受一个数组并返回一个对象。


文件名:reducers/index.js

代码:

import { combineReducers } from 'redux';
import authReducer from './authReducer';
import { reducer as formReducer } from 'redux-form';
import postsReducer from './postsReducer';
import groupsReducer from './groupsReducer';

export default combineReducers ({
 auth: authReducer,
 form: formReducer,
 posts: postsReducer,
 groups: groupsReducer,
 });

文件名:GroupList.js

代码:

  import React from "react";     
  import { connect } from "react-redux";
  import { getGroups } from '../../actions';

  class GroupList extends React.Component {

   componentDidMount() {
     this.props.getGroups();
    }

    render() {
     return <div>
       <h1>GroupList</h1>
       </div>;
     }
    }


   export default connect(null, { getGroups })(GroupList);

文件名:App.js

代码:

import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose} from 'redux';
import reduxThunk from 'redux-thunk';
import reducers from './reducers';
import Landing  from './components/Landing';


const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
  reducers,
  composeEnhancers(applyMiddleware(reduxThunk))
);

ReactDOM.render(
  <Provider store={store}>
    <Landing />
  </Provider>,
 document.querySelector('#root')
);```






Ive connected to ReduxDevToolsExtension in chrome and when i check the state in there, it shows empty.

【问题讨论】:

    标签: javascript reactjs redux react-redux redux-thunk


    【解决方案1】:

    您在 switch-case 中使用了错误的参数。

    将switch (action.payload) 替换为switch (action.type)

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      相关资源
      最近更新 更多