Redux获取JSON数据

时间:2017-11-29 05:45:33

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

我的Redux fetch返回空.. 它不会破坏,但它只会让我返回空对象。

以下是我的行动代码(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);




有谁能告诉我为什么这会破裂? 还有,有人能建议我更好/更清楚地编写这些代码吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我在你的减速机中预感

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

应该是

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

坦白说它应该只是

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

由于我对你的商业逻辑没有任何了解,但后来对我来说似乎更正确(为什么你需要将它与旧状态合并)

您需要在州内传播action.payload.data