Reactjs Redux获取未通过
我正在尝试将Redux添加到我的测试应用程序中,但从API获取数据时遇到问题。感觉上我已经完成了所有的步骤,但是我无法从我的组件中获取道具,所以我在某个地方搞砸了。我的代码: actions/index.js:Reactjs Redux获取未通过,reactjs,redux,fetch,Reactjs,Redux,Fetch,我正在尝试将Redux添加到我的测试应用程序中,但从API获取数据时遇到问题。感觉上我已经完成了所有的步骤,但是我无法从我的组件中获取道具,所以我在某个地方搞砸了。我的代码: actions/index.js: import 'whatwg-fetch'; import ReduxThunk from 'redux-thunk'; export const FETCH_RECIPES = 'FETCH_RECIPES'; const ROOT_URL = 'http://myapi/recip
import 'whatwg-fetch';
import ReduxThunk from 'redux-thunk';
export const FETCH_RECIPES = 'FETCH_RECIPES';
const ROOT_URL = 'http://myapi/recipe/';
export function fetchRecipes(id) {
const url = ROOT_URL + "0";
// const request = fetch(url);
return (dispatch) => {
fetch(url)
.then((response) => response.json())
.then((request) => dispatch(fetchRecipesSuccess(request)))
};
}
export function fetchRecipesSuccess(request) {
return {
type: FETCH_RECIPES,
request
};
}
import { combineReducers } from 'redux';
import RecipesReducer from './reducer_recipes';
const rootReducer = combineReducers({
recipes: RecipesReducer
})
export default rootReducer;
reducer_recipe.js:
import { FETCH_RECIPES } from "../actions/index";
export default function(state = [], action) {
switch(action.type) {
case FETCH_RECIPES:
return [action.payload.data, ...state];
}
return state;
}
reducers/index.js:
import 'whatwg-fetch';
import ReduxThunk from 'redux-thunk';
export const FETCH_RECIPES = 'FETCH_RECIPES';
const ROOT_URL = 'http://myapi/recipe/';
export function fetchRecipes(id) {
const url = ROOT_URL + "0";
// const request = fetch(url);
return (dispatch) => {
fetch(url)
.then((response) => response.json())
.then((request) => dispatch(fetchRecipesSuccess(request)))
};
}
export function fetchRecipesSuccess(request) {
return {
type: FETCH_RECIPES,
request
};
}
import { combineReducers } from 'redux';
import RecipesReducer from './reducer_recipes';
const rootReducer = combineReducers({
recipes: RecipesReducer
})
export default rootReducer;
AAAA在我的组件中,我使用以下代码:
function mapStateToProps({ recipes }) {
return { recipes };
}
connect(mapStateToProps, {fetchRecipes})(Recipe);
在我的index.js中,我使用constcreatestorewithmiddleware=createStore(reducers,applyMiddleware(ReduxPromise))创建我的存储代码>
我认为我应该很好地使用this.props来访问从API中获取的数据,但我想我正在将数据放在某个地方。我遗漏了什么?检查好减速器。您似乎正在返回action.payload.data
,而在fetchRecipesSuccess
中,它被命名为request
。您可以将action对象记录到console.log中,查看您得到了什么
import { FETCH_RECIPES } from "../actions/index";
export default function(state = [], action) {
switch(action.type) {
case FETCH_RECIPES:
// Verify here that your request object has data
return [...state, action.request.data];
// Default state
default:
return state;
}
}
希望这有帮助 是的,好好检查你的减速器。您应该返回action.request.data
。您还可以console.log操作对象来查看您得到了什么感谢您的快速回答!这是我的一大失误。尽管如此,当我运行console.log(action.request.data)时,什么也没发生——就好像我的应用程序中没有运行reducer一样。我是否必须以某种方式将其连接起来?您能否验证您是否以reducers
的方式导入了您的reducers,即它在这里的方式=>const createStoreWithMiddleware=createStore(reducers,applyMiddleware(ReduxPromise))代码>是的,这就是我的代码的外观,我正在用一个
包装我的代码。我改为applyMiddleware(thunk)
,因为我一直在尝试使用它,但它仍然不起作用。在reducer中,它进入默认情况,而不是case FETCH\u RECIPES
。。。我迷路了!等等,我正在测试一些东西。如果您可以使用codesandbox.io来共享它,那就好了。只需将api调用替换为模拟数据。