2016-12-24 16 views
0

大家和歡快的X-MAS我創建 和源代碼這樣的動作:行動不mapDispatchtoProps映射

export const SWITCH_LANGUAGE = 'SWITCH_LANGUAGE'; 

    export function switchLanguage(language) { 

     return { 
      type: SWITCH_LANGUAGE, 
      payload: language 
     }; 
    } 

然後我想做出這個動作的道具在我page.js 作爲結果我寫:

import { switchLanguage } from '../actions/index'; 
... 
function mapDispatchToProps(dispatch) { 
    return bindActionCreators({ switchLanguage }, dispatch); 
} 
.. 
const switchLanguage = this.props.switchLanguage; 

但是,當我寫的console.log(props.switchLanguage)它讓我

function() { 
     return dispatch(actionCreator.apply(undefined, arguments)); 
     } 

這是怎麼發生的?

+0

我這是問題const switch語言= this.props.switchLanguage(//參數) – Codesingh

+0

一個參數?我不這麼認爲,但感謝您的快速回復:) – user7334203

+0

所以有什麼問題? – Codesingh

回答

0

您正在獲得的輸出是預期的。那裏沒有錯。行:

function() { 
    return dispatch(actionCreator.apply(undefined, arguments)); 
} 

是行動的創建者(switchLanguage)與應用參數傳遞的任何(與apply),那麼它傳遞到dispatch。這正是bindActionCreators的用途。

根據該文檔,bindActionCreators是當你想通過一些行動創下來就是不知道 終極版的一個組件使用,你不想通過調度或終極版商店它。 查看更多在http://redux.js.org/docs/api/bindActionCreators.html

在你的組件中,你可以像你剛纔那樣訪問任何道具。然後,如果您以前從this.props中提取了該功能,則只需撥打this.props.switchLanguage(/*language*/)switchLanguage(/*language*/)即可。

只要減速器正確處理動作,一切都應按預期工作。