Redux取不通过

时间:2017-08-09 13:48:14

标签: reactjs redux fetch

我正在尝试将Redux添加到我的测试应用中,但是我无法从我的API中获取数据。感觉就像我已经完成了所有的步骤,但是我无法从我的组件中的fetch访问道具,所以我在路上的某个地方搞乱了。我的代码:

动作/ 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
    };
}

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

减速器/ index.js:

import { combineReducers } from 'redux';
import RecipesReducer from './reducer_recipes';

const rootReducer = combineReducers({
    recipes: RecipesReducer
})

export default rootReducer;

aaaaand在我的组件中我正在使用此代码:

function mapStateToProps({ recipes }) {
    return { recipes };
}

connect(mapStateToProps, {fetchRecipes})(Recipe);

在我的index.js中,我用const createStoreWithMiddleware = createStore(reducers, applyMiddleware(ReduxPromise));

创建我的商店

根据我的想法,我应该很好地使用this.props来访问我从我的API获取的数据,但我想我一直在丢弃数据。我错过了什么?

1 个答案:

答案 0 :(得分:1)

检查您的减速机。您似乎正在返回#include <iostream> #include <iterator> using namespace std; int main() { int a=15; const int*ap=&a; allocator<iterator_traits<decltype(ap)>::value_type>aallo; cout<<"before construct:"<<a<<endl; //15 aallo.construct(ap,27); //isn't illegal? cout<<"after construct:"<<a<<endl; //27 } ,而在action.payload.data中,它的名称为fetchRecipesSuccess。您可以通过console.log操作对象来查看您的内容

request

希望这有帮助!