[英]Understanding mapStateToProps & mapDispatchToProps in React-Redux
I'm trying to understand the connect()
method of react-redux .我试图了解react-redux的
connect()
方法。 Usually it takes two function as argument: mapStateToProps()
& mapDispatchToProps()
.通常它需要两个 function 作为参数:
mapStateToProps()
& mapDispatchToProps()
。 I write a example for myself, here is connect()
section of my User
component:我为自己编写了一个示例,这是我的
User
组件的connect()
部分:
//imports...
class User extends Component {
/* constructor, JSX, other functions... */
}
const mapStateToProps = (state) => {
return {
users: state.UserReducer
};
};
const mapDispatchToProps = (dispatch) => ({
deleteUser: (id) => dispatch(deleteUser(id))
});
export default connect(mapStateToProps, mapDispatchToProps)(User);
According to Docs I have taken the following two conclusions about mapStateToProps()
& mapDispatchToProps()
:根据 文档,我对
mapStateToProps()
和mapDispatchToProps()
得出以下两个结论:
reducer
to component.reducer
传递给组件。action
functions, ie with this function We can perform the action
that we want in our component
. action
函数,即使用这个 function 我们可以在我们的component
中执行我们想要的action
。 is my conclusions correct?我的结论正确吗?
React components accept data from outside via props
. React 组件通过
props
接受来自外部的数据。 maptStateToProps
and mapDispatchToProps
, literally, pass the selected state properties and actions that are needed inside your component as props. maptStateToProps
和mapDispatchToProps
从字面上看,将组件内所需的选定 state 属性和操作作为道具传递。 The state values and actions passed to the component are available in the props of the component.传递给组件的 state 值和操作在组件的 props 中可用。 In your example, you can use
this.props.users
or this.props.deleteUser()
.在您的示例中,您可以使用
this.props.users
或this.props.deleteUser()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.