gpt4 book ai didi

reactjs - 调度和bindActionCreators有什么区别?

转载 作者:行者123 更新时间:2023-12-03 13:07:23 26 4
gpt4 key购买 nike

If we are connecting to the action by using the dispatch there are two way:-

1. this.props.dispatch(requestEmployees());

2. const mapDispatchToProps = (dispatch) => ({
requestEmployees: () => dispatch(requestEmployees())

});

If we are doing the same with the help of bindActionCreators then our code we will be:-

function matchDispatchToProps(dispatch) {
return bindActionCreators({ editLabResult: requestEmployees}, dispatch);
}

Now my question is, which one I should use dispatch or bindActionCreators? What is the difference between them?

最佳答案

其实是同一件事。结果

bindActionCreators({ editLabResult: requestEmployees}, dispatch);

是您手动创建的:

requestEmployees: () => dispatch(requestEmployees())
<小时/>

根据redux bindActionCreators文档:

Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch call so they may be invoked directly.

bindActionCreators({ editLabResult: requestEmployees, anotherAction, etc... }, dispatch);

您可以将对象传递给connect,而不是使用bindActionCreators。方法,它会为你完成包装:

connect(mapStateToProps, { editLabResult: requestEmployees, anotherAction, etc... })

关于reactjs - 调度和bindActionCreators有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41342540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com