是的,这是正确的。它只是一个帮助器函数,具有更简单的方法来访问状态属性
想象一下,你的应用中有一把钥匙posts
state.posts
state.posts //
/*
{
currentPostId: "",
isFetching: false,
allPosts: {}
}
*/
和组件Posts
默认情况下,将使所有状态属性都可用于连接的组件connect()(Posts)
const Posts = ({posts}) => (
<div>
{/* access posts.isFetching, access posts.allPosts */}
</div>
)
现在,当您将 映射到组件时,它会变得更好一些state.posts
const Posts = ({isFetching, allPosts}) => (
<div>
{/* access isFetching, allPosts directly */}
</div>
)
connect(
state => state.posts
)(Posts)
mapDispatchToProps
通常你必须写dispatch(anActionCreator())
与你可以做它也更容易像bindActionCreators
connect(
state => state.posts,
dispatch => bindActionCreators({fetchPosts, deletePost}, dispatch)
)(Posts)
现在,您可以在组件中使用它
const Posts = ({isFetching, allPosts, fetchPosts, deletePost }) => (
<div>
<button onClick={() => fetchPosts()} />Fetch posts</button>
{/* access isFetching, allPosts directly */}
</div>
)
操作创建器的更新。.
操作创建器的示例:deletePost
const deletePostAction = (id) => ({
action: 'DELETE_POST',
payload: { id },
})
所以,只会采取你的行动,把它们包装成电话。(我没有阅读redux的源代码,但实现可能看起来像这样:bindActionCreators
dispatch
const bindActionCreators = (actions, dispatch) => {
return Object.keys(actions).reduce(actionsMap, actionNameInProps => {
actionsMap[actionNameInProps] = (...args) => dispatch(actions[actionNameInProps].call(null, ...args))
return actionsMap;
}, {})
}