2016-09-21 30 views
1

對於Redux及其概念,特別是中間件,我真的很陌生,所以我對任何愚蠢的錯誤表示歉意。繼續收到'dispatch'undefined與redux-promise

在這個項目中我的,我需要使用終極版-的thunk。我已經看過一些關於如何應用它們的指南和解釋。然後我不斷收到一個錯誤「Uncaught TypeError:無法讀取屬性'調度'的未定義」。我打開了開發工具,結果被顯示此錯誤:

enter image description here

我不知道,如果我做任何事情的權利。以下是我的動作創作者和商店的代碼。

動作/ index.js

import axios from 'axios'; 

export function fetchLessons() { 
    console.log('called!'); 
    return function(dispatch) { 
    axios.get(`${ROOT_URL}/lessons`) 
     .then((response) => { 
     dispatch(fetchLessonsSuccess(response)) 
     }) 
     .catch((err) => { 
     dispatch(fetchLessonsError(err)) 
     }) 
    } 
} 

function fetchLessonsError(){ 
    return "An error has occured"; 
} 

function fetchLessonsSuccess(response) { 
    return { 
    type: FETCH_LESSONS, 
    payload: request 
    }; 
} 

index.js(店)

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import { Provider } from 'react-redux'; 
import { createStore, applyMiddleware, compose } from 'redux'; 
import { Router, browserHistory } from 'react-router'; 
import rootReducer from './reducers/index'; 
import routes from './routes'; 
import promise from 'redux-promise'; 
import thunk from 'redux-thunk'; 

const middleware = applyMiddleware(promise(), thunk); 
const store = createStore(rootReducer, compose(middleware)); 

ReactDOM.render(
    <Provider store={store}> 
    <Router history={browserHistory} routes={routes} /> 
    </Provider> 
    , document.querySelector('.container')); 

回答

3

我相信你到applyMiddleware()電話是稍微偏離。您希望直接傳入導入的承諾中間件,而不是將其稱爲:applyMiddleware(promise, thunk)

該功能基本上是一個工廠。 Redux會調用它並傳入商店的dispatch函數,中間件隨後可以使用該函數在準備就緒時分派操作。

0

我不完全確定,但像這樣

export function fetchLessons() { 
    console.log('called!'); 
    return function(dispatch) { 
    return dispatch({ 
     type: 'FETCH_LESSONS', 
     payload: axios.get(`${ROOT_URL}/lessons`) 
     .then((response) => { 
      dispatch(fetchLessonsSuccess(response)) 
     }) 
     .catch((err) => { 
      dispatch(fetchLessonsError(err)) 
     }); 
    }); 
    }; 
} 

function fetchLessonsError(){ 
    return "An error has occured"; 
} 

function fetchLessonsSuccess(response) { 
    return { 
    type: 'FETCH_LESSONS_FULFILLED', 
    payload: response 
    }; 
}