【问题标题】:Redux Fetch JSON DataRedux 获取 JSON 数据
【发布时间】:2018-05-12 18:06:46
【问题描述】:

我的 Redux 提取返回空.. 它不会中断,但只会返回空对象。

这是我的操作 (newsActions.js) 的代码:

import axios from 'axios';
import kickstarterData from '../server/kickstarter-october.json';

export const FETCH_KICKSTARTER = 'FETCH_KICKSTARTER';

export function fetchKickstarter() {

	return {
		type: FETCH_KICKSTARTER,
		payload: {
			data: kickstarterData
		}
	};
}

这是我的减速器:

import { FETCH_KICKSTARTER } from '../actions/kickstarterActions';


export default function(state = [], action) {
	switch (action.type) {
		case FETCH_KICKSTARTER:
			debugger;
			return [action.payload.data, ...state];
	}

	return state;
};
https://stackoverflow.com/questions/ask#

这是我的 index.js,它结合了所有的 reducer:

import { combineReducers } from 'redux';
import NewsReducer from './reducer_news';
import KickstarterReducer from './reducer_kickstarter';

const rootReducer = combineReducers({
  news: NewsReducer,
  kickstarters: KickstarterReducer
});

export default rootReducer;

最后,在我的 app.js 中,我有以下代码:

const mapStateToProps = (state) => ({
    news: state.news,
    kickstarters: state.kickstarters
});


export default connect(mapStateToProps, {...newsActions, ...kickstarterActions})(App);

谁能告诉我为什么会这样? 另外,任何人都可以建议我编写这些代码的更好/更清洁的方式吗?

谢谢

【问题讨论】:

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


    【解决方案1】:

    我有预感在你的减速器中

    return [action.payload.data, ...state];

    应该是

    return [...action.payload.data, ...state];

    坦率地说应该只是

    return [...action.payload.data ];

    因为我对你的商业逻辑一无所知,但后来对我来说似乎更正确(你为什么需要将它与旧状态合并)

    你需要在状态中传播action.payload.data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多