gpt4 book ai didi

javascript - Redux 获取 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 03:55:51 25 4
gpt4 key购买 nike

我的 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
}
};
}

这是我的 reducer :

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);

谁能告诉我为什么会这样?另外,有人可以建议我一种更好/更干净的方式来编写这些代码吗?

谢谢

最佳答案

我有预感,在你的 reducer 中

返回[action.payload.data, ...state];

应该是

返回[...action.payload.data, ...state];

说实话应该是这样

返回[...action.payload.data];

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

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

关于javascript - Redux 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47545882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com