2016-12-24 49 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*/)即可。

只要减速器正确处理动作,一切都应按预期工作。