1. react-redux作用?
(1).yarn add .react-redux
(2).使用connect()()创建并暴露一个Count的容器组件,来链接UI组件和Redux(store);
在UI组件中就可以用 props 接收状态;并且不需要再检测组件处是否重新渲染,connect 会根据状态的改变,自动渲染组件
2. react-redux组成:
1)容器组件 ------ connect(mapStateToProps,mapDispatchToProps)(CountUI)
a. 左手连接 “UI组件库”,通过props传参的方式给予UI组件state和更改state的回调;
connect(
(state)=>({
count:state}),
{
handleAdd: addCount,
handleReduce: reduceCount,
handleAsyncAdd: addAsyncCount,
}
)(CountUI)
b. 右手连接 “redux”,使用store.dispatch(action)/store.getState()与其通信
2) UI组件 ------ 不能使用任何redux的api,只负责页面的呈现、交互等。
3) store --------- 是靠props传给容器组件,而不是在容器组件中直接引入
import {
Provider } from "react-redux";
<Provider store={
store}>
<App />
</Provider>