2016-03-03 61 views
0

有人能看到有什麼不對我的thunk?內部代碼永遠不會被調用,但外部代碼是。這是一個例子的thunk:的thunk不派遣

export function selectCustomer(customerId) { 
    console.log("This appears in the console fine"); 
    return (dispatch, getState) => { 
     console.log("This doesn't.. why don't you run..?"); 
     dispatch(loadCustomerToEdit(customerId)); 
    } 
}; 

這是我如何包裝它的組件事件:

import React, { Component, PropTypes } from 'react'; 
import CustomerEditForm from './CustomerEditForm.jsx'; 
import { editCustomer, selectCustomer, selectNewCustomer, saveCustomer } from '../redux/action_creators.jsx'; 


export const CustomerContainer = React.createClass({ 
    componentWillMount() { 
     const customerId = FlowRouter.getParam('_id'); 

     if (customerId) { 
      this.sub = Meteor.subscribe('CustomerCompany.get', customerId, this.setCustomerInState); 
     } else { 
      this.props.selectNewCustomer(); 
     } 
    }, 

    setCustomerInState() { 
     console.log("setCustomerInState"); 
     this.props.selectCustomer(FlowRouter.getParam('_id')); 
    }, 

    // Snip 

    render() { 
     console.log("CustomerContainer.render()", this.props); 
     if (this.sub && !this.sub.ready) { 
      return (<h1>Loading</h1>); 
     } 
     return (
      <CustomerEditForm 
       customer = {this.props.customer} 
       onChange = {this.props.onChange} 
       onSave = {this.props.onSave} 
       errors = {this.props.customer.errors} 
       isValid = {this.props.customer.isValid} 
       salesRegionOptions={SalesRegions.find().fetch()} 
      /> 
     ); 
    } 
}); 

CustomerContainer.propTypes = { 
    customer: PropTypes.object, 
    onSave: PropTypes.func.isRequired, 
    onChange: PropTypes.func.isRequired, 
    selectCustomer: PropTypes.func.isRequired, 
    selectNewCustomer: PropTypes.func.isRequired 
}; 

function mapStateToProps(state) { 
    console.log("CustomerContainer.mapStateToProps", state) 
    return { 
     customer: state.userInterface.customerBeingEdited 
    }; 
} 

function mapDispatchToProps(dispatch) { 
    //console.log("CustomerContainer.mapDispatchToProps", Actions.customerSave) 
    return { 
     onSave: saveCustomer, 
     onChange: editCustomer, 
     selectCustomer, 
     selectNewCustomer 
    }; 
} 

export default connect(mapStateToProps, mapDispatchToProps 
)(CustomerContainer); 

這是我的商店設置:

import { createStore, combineReducers, applyMiddleware, compose } from 'redux'; 
import rootReducer from './reducers.jsx'; 
import thunk from 'redux-thunk'; 

const middleware = [ thunk ] 

const createStoreWithMiddleware = applyMiddleware(...middleware)(createStore) 
const store = createStoreWithMiddleware(rootReducer) 

export default store; 

你」毫無疑問,我們會認識到很多代碼,因爲它是從優秀的examples in the redux documentation改編而來的。

selectCustomer函數被調用,所以mapDispatchToProps似乎有線的selectCustomer功能組件,它只是通過selectCustomer返回的方法不叫。

+1

什麼是你的終極版的版本?該'createStore' API在終極版> = 3.1.0改了一下,現在是: '常量店= createStore( rootReducer, applyMiddleware(咚) );' – mxstbr

+0

啊,那是很好的瞭解@mstoiber,謝謝您。我使用的是React 14.7,Redux 3.3.1和Redux-Thunk 1.0.3。我只是轉向新的語法,但仍然有同樣的問題。 – tomRedox

回答

1

的問題是你的mapDispatchToProps功能。 react-redux不會自動包裝您的動作創建者,只要您將它傳遞給一個函數即可! (或者如果你手動綁定他們bindActionCreators

試着改變你的connect調用此,它應該工作:

connect(mapStateToProps, { 
    onSave: saveCustomer, 
    onChange: editCustomer, 
    selectCustomer, 
    selectNewCustomer 
})(YourComponent); 
+0

那是語法我原本,但終極版的例子移動調度的操作方法(我認爲他們被稱爲動作的創造者),所以如果你看一下,做派遣我的'selectCustomer'方法。從文檔,這是容器的一個例子:https://github.com/reactjs/redux/blob/master/examples/shopping-cart/containers/ProductsContainer.js,這是動作的一個示例:HTTPS: //github.com/reactjs/redux/blob/master/examples/shopping-cart/actions/index.js。他們不是在做'mapDispatchToProps'那裏派遣這正是我試圖複製 – tomRedox

+0

權,但你的'selectCustomer'功能出於某種原因需要訪問該'dispatch' - 它獲得通過分派。現在,您擁有的另一個選項是在'connect'函數中簡單地使用一個對象,這就是redux示例所做的。我已經編輯了上面的答案來反映這一點! – mxstbr

+0

謝謝,就是這樣。換句話說,我需要實際調用'mapDispatchToProps'在我的原代碼,即堅持'()'在年底做'出口默認連接(mapStateToProps,mapDispatchToProps() )(CustomerContainer);' – tomRedox