gpt4 book ai didi

reactjs - mapStateToProps 与 mapDispatchToProps

转载 作者:行者123 更新时间:2023-12-03 13:00:46 24 4
gpt4 key购买 nike

mapStateToProps 之间有什么区别 mapDispatchToProps connect 的参数在react-redux中函数?

最佳答案

mapStateToProps 是一个用于向组件提供存储数据的函数,而 mapDispatchToProps 是您用于向组件提供操作创建者作为 props 的函数。你的组件。

根据文档:

If mapStateToProps argument is specified, the new component will subscribe to Redux store updates. This means that any time the store is updated, mapStateToProps will be called. The results of mapStateToProps must be a plain object, which will be merged into the component’s props.

With mapDispatchToProps every action creator wrapped into a dispatch call so they may be invoked directly, will be merged into the component’s props.

一个简单的例子是

function mapStateToProps(state) {
return { todos: state.todos }
}

function mapDispatchToProps(dispatch) {
return { addTodo: bindActionCreators(addTodo, dispatch) }
}

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

关于reactjs - mapStateToProps 与 mapDispatchToProps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49994197/

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