React — Redux详解
<div><div id="scrollRef" class="h-full overflow-hidden overflow-y-auto">
<div id="image-wrapper" class="w-full max-w-screen-xl m-auto dark:bg-[#101014] p-4">
<div class="flex w-full mb-6 overflow-hidden">
<div class="overflow-hidden text-sm items-start">
<div class="flex items-end gap-1 mt-2 flex-row">
<div class="text-black text-wrap min-w- rounded-md px-3 py-2 bg-[#f4f6f8] dark:bg-[#1e1e20]">
<div class="leading-relaxed break-words">
<div class="markdown-body">
<p><span style="font-size: 16px"><strong>Redux 是一个用于 JavaScript 应用程序的状态管理库。它可以帮助您管理应用程序中的状态,并确保状态的一致性和可预测性。</strong></span></p>
<p><span style="font-size: 16px"><strong>Redux 主要用于处理大型应用程序中的复杂状态逻辑,例如<span style="color: rgba(255, 0, 0, 1)">跨组件共享状态、处理异步数据流</span>等。</strong></span></p>
<p><span style="font-size: 16px"><strong>Redux 的核心概念包括:</strong></span></p>
<ol>
<li>
<p>Store(存储):Redux 应用程序的状态(state)被统一存储在一个称为 Store 的对象中。这个状态是只读的,唯一改变状态的方式是通过分发(dispatching)一个 action。</p>
</li>
<li>
<p>Action(动作):Action 是描述发生了什么事情的纯 JavaScript 对象。它必须包含一个 <code>type</code> 属性来指明动作的类型,通常还会包含一些与动作相关的数据。</p>
</li>
<li>
<p>Reducer(归纳器):Reducer 是一个纯函数,接收先前的状态和一个 action,并返回新的状态。Reducer 负责根据 action 更新应用程序的状态。</p>
</li>
<li>
<p>Dispatch(分发):使用 <code>store.dispatch(action)</code> 方法来分发一个 action,这是唯一能改变状态的方法。</p>
</li>
<li>
<p>Selector(选择器):选择器用于从 Store 中提取部分状态数据,以便在应用程序的组件中使用。</p>
</li>
</ol>
<p><strong>Redux 的工作流程是:当应用程序中的某个地方发生了变化,比如用户交互或者网络请求返回数据,就会创建一个对应的 action 并被分发到 store 中。然后,相应的 reducer 会根据 action 更新 store 中的状态。最后,React 组件可以订阅 store 中的状态,并根据状态的变化来更新界面。</strong></p>
<p><strong>通过这种方式,Redux 提供了一种可预测且一致的状态管理机制,使得应用程序的状态变化变得容易追踪和调试。这使得 Redux 成为处理大型复杂应用程序状态的理想选择。</strong></p>
</div>
</div>
</div>
<div class="flex flex-col"> </div>
</div>
</div>
</div>
</div>
</div>
</div>
<p><strong><span style="font-size: 16px">一、CDN引入JS使用Redux</span></strong></p>
<div>
<p>1.定义一个reducer函数(根据当前想要做的修改返回一个新的状态)</p>
<p>2.使用createStore方法传入reducer函数 生成一个store实例对象</p>
<p>3.使用store实例的subscribe订阅数据的变化(数据一旦变化,可以得到通知)</p>
<p>4.使用store实例的dispatch方法提交action对象触发数据变化(告诉reducer你想怎么改数据)(修改数据的唯一方式)</p>
<p>5.使用store实例的getState方法获取最新的状态数据更新到视图中</p>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307160510005-600934216.jpg"></p>
<div>
<div class="cnblogs_code">
<pre><script src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://cdnjs.cloudflare.com/ajax/libs/redux/4.1.1/redux.min.js</span><span style="color: rgba(128, 0, 0, 1)">"</span>></script>
<div <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sum</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<button id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">reduce</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
-
</button>
<span id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">num</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<span style="color: rgba(128, 0, 128, 1)">0</span>
</span>
<button id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
+
</button>
</div>
<script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.定义reducer函数 : 根据不用的action函数,返回不同的state
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">state:管理数据的初始状态
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">action:对象 type 标记当前想做什么样的修改</span>
function reducer(state={count:<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">},action){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据不可变,基于原始状态生成新的状态</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(action.type === <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">INCREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> {count:state.count+<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">}
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(action.type === <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DECREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> {count:state.count-<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> state
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.使用createStore方法传入reducer函数 生成一个store实例对象</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> store =<span style="color: rgba(0, 0, 0, 1)"> Redux.createStore(reducer)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.通过store实例的subscribe订阅数据变化</span>
store.subscribe(()=><span style="color: rgba(0, 0, 0, 1)">{
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">state变化了</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,store.getState())
document.getElementById(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">num</span><span style="color: rgba(128, 0, 0, 1)">'</span>).innerText=<span style="color: rgba(0, 0, 0, 1)">store.getState().count
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4.使用store实例的dispatch方法提交action对象触发数据变化</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> inBtn = document.getElementById(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">add</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">const</span> dBtn = document.getElementById(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">reduce</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
inBtn.addEventListener(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">click</span><span style="color: rgba(128, 0, 0, 1)">'</span>,()=><span style="color: rgba(0, 0, 0, 1)">{
store.dispatch({
type:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">INCREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
})
})
dBtn.addEventListener(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">click</span><span style="color: rgba(128, 0, 0, 1)">'</span>,()=><span style="color: rgba(0, 0, 0, 1)">{
store.dispatch({
type:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">DECREMENT</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
})
})
</span></script></pre>
</div>
<p> </p>
</div>
</div>
<p><span style="font-size: 16px"><strong>二、creatReactApp(框架)使用Redux</strong></span></p>
<p>1.创建环境</p>
<div class="cnblogs_code">
<pre>npm i @reduxjs/toolkit react-redux</pre>
</div>
<p>(1) Redux Toolkit(RTK) 官方推荐编写Redux逻辑的方式,是一套工具的集合集,简化书写方式</p>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307161351329-973781249.png"></p>
<p>(2)react-redux 用来链接Redux和React组件的中间件</p>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307161958030-327428963.png"></p>
<p>(3)目录结构设计</p>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307162634251-1931556801.png"></p>
<p>2.使用方式</p>
<p>(1)配置子模块(子store)</p>
<div class="cnblogs_code">
<pre>import {createSlice} <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@reduxjs/toolkit</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">createSlice 创建store的方法</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> countStore =<span style="color: rgba(0, 0, 0, 1)"> createSlice({
name : </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">counter</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给模块一个名字</span>
<span style="color: rgba(0, 0, 0, 1)"> initialState:{
count : </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">
}, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化state</span>
<span style="color: rgba(0, 0, 0, 1)"> reducers :{
add(state){
state.count</span>++<span style="color: rgba(0, 0, 0, 1)">
},
reduce(state){
state.count</span>--<span style="color: rgba(0, 0, 0, 1)">
},</span><span style="color: rgba(0, 0, 0, 1)">
}</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改状态的方法 同步方法 支持直接修改</span>
<span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">解构出来actionCreater函数==》解构出来的方法叫做actionCreater (也就是说这些方法是生成action对象的)</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> {add,reduce} =<span style="color: rgba(0, 0, 0, 1)"> countStore.actions
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取reducer函数</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> reducer =<span style="color: rgba(0, 0, 0, 1)"> countStore.reducer
//导出创建action对象的函数和reducer函数
export {add,reduce,addNum}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> reducer</pre>
</div>
<p>(2)index.js入口文件配置根store并组合子store</p>
<div class="cnblogs_code">
<pre>import {configureStore} <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@reduxjs/toolkit</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">组合子模块的方法</span>
import countReducer <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./modules/counterStore</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导入子模块</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">配置</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> store=<span style="color: rgba(0, 0, 0, 1)">configureStore({
reducer:{
counter : countReducer,
}
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导出</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> store</pre>
</div>
<p>(3)在react中注入store</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">import store </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./store/index</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
import {Provider} </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">react-redux</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">react-redux负责把Redux和React连接起来,内置Provider组件通过store参数把创建好的store实例注入到应用中,链接正式建立
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把App根组件渲染到id为root的dom节点</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> root = ReactDOM.createRoot(document.getElementById(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
root.render(
</span><Provider store={store}>
<App />
</Provider><span style="color: rgba(0, 0, 0, 1)">
);</span></pre>
</div>
<p>(4)使用store中的数据 修改store中的数据</p>
<div class="cnblogs_code">
<pre>import { useSelector,useDispatch } <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">react-redux</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在React组件中使用store的数据,需要用到一个钩子函数useSelector,他的作用是把store中的数据映射到组件中</span>
<span style="color: rgba(0, 0, 0, 1)">
import {add,reduce} </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./store/modules/counterStore</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
//导入actionCreater<br>
function App(){
</span><span style="color: rgba(0, 0, 255, 1)">const</span> {count} = useSelector(state=><span style="color: rgba(0, 0, 0, 1)">state.counter)
</span><span style="color: rgba(0, 0, 255, 1)">const</span> dispatch =<span style="color: rgba(0, 0, 0, 1)"> useDispatch()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">React组件中修改store中的数据需要借助另一个hook函数 useDispatch ,他的作用是生成提交action对象的dispatch函数</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> <div>
<div>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(reduce())
}}</span>>-</button>
<span>{count}</span>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(add())
}}</span>>+</button>
</div>
</div><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307170607975-2004194631.jpg"></p>
<p><img src="https://img2024.cnblogs.com/blog/1483877/202403/1483877-20240307170655853-740248858.png"></p>
<p>(5)总结</p>
<p>组件中使用useSelector (hook函数)获取store中的对象</p>
<p>组件中使用useDispatch(hook函数)获取dispatch方法</p>
<p>执行store模块中导出的actionCreater方法就能得到要提交的action对象</p>
<p>3.action传参</p>
<p>(1)在reducer的同步修改方法中添加action对象参数</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> countStore =<span style="color: rgba(0, 0, 0, 1)"> createSlice({
name : </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">counter</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给模块一个名字</span>
<span style="color: rgba(0, 0, 0, 1)"> initialState:{
count : </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">
}, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化state</span>
<span style="color: rgba(0, 0, 0, 1)"> reducers :{
add(state){
state.count</span>++<span style="color: rgba(0, 0, 0, 1)">
},
reduce(state){
state.count</span>--<span style="color: rgba(0, 0, 0, 1)">
},
addNum(state,action){
state.count</span>= state.count+<span style="color: rgba(0, 0, 0, 1)">action.payload</span></pre>
<div> //参数会被传递到action对象的payload属性上</div>
<pre><span style="color: rgba(0, 0, 0, 1)"> } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在reducer的同步修改方法中添加action对象参数</span>
}<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改状态的方法 同步方法 支持直接修改</span>
<span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">解构出来actionCreater函数==》解构出来的方法叫做actionCreater (也就是说这些方法是生成action对象的)</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> {add,reduce,addNum} =<span style="color: rgba(0, 0, 0, 1)"> countStore.actions
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取reducer函数</span>
<span style="color: rgba(0, 0, 255, 1)">const</span> reducer =<span style="color: rgba(0, 0, 0, 1)"> countStore.reducer
export {add,reduce,addNum}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> reducer</pre>
</div>
<p>(2)调用actionCreater的时候传递参数,参数会被传递到action对象的payload属性上</p>
<div class="cnblogs_code">
<pre><div>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(reduce())
}}</span>>-</button>
<span>{count}</span>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(add())
}}</span>>+</button>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(addNum(</span><span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">))
}}</span>>
+<span style="color: rgba(128, 0, 128, 1)">10</span>
</button>
<button onClick={()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(addNum(</span><span style="color: rgba(128, 0, 128, 1)">20</span><span style="color: rgba(0, 0, 0, 1)">))
}}</span>>
+<span style="color: rgba(128, 0, 128, 1)">20</span>
</button>
</div>
<span style="color: rgba(0, 128, 0, 1)">//调用actionCreater的时候传递参数</span></pre>
</div>
<p>4.异步操作</p>
<p>(1)配置store</p>
<p>(2)同步修改</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> channelStore=<span style="color: rgba(0, 0, 0, 1)">createSlice({
name : </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">channel</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, //定义子store的名字
initialState :{
channelList : [] //初始化数据的状态
},
reducers:{<br> //定义修改数据的同步方法
setList(state,action){
state.channelList </span>=<span style="color: rgba(0, 0, 0, 1)"> action.payload
}
}
})</span></pre>
</div>
<p>(3)异步修改</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">异步请求<br></span></pre>
<div> const {setList} = channelStore.actions</div>
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> channelAsync=()=><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">async</span> (dispatch)=><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">const</span> res = <span style="color: rgba(0, 0, 255, 1)">await</span> axios.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">'。。。</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) //封装异步请求获取数据
dispatch(setList(res.data.data.channels)) //调用同步actionCreater传入异步数据生成的一个action对象,并使用dispatch提交
}
}</span></pre>
</div>
<p>(4)使用store的数据</p>
<p>(5)diapatch提交action对象更新store</p>
<div class="cnblogs_code">
<pre></pre>
<div> import {channelAsync} from './store/modules/channelStore'</div>
<pre><span style="color: rgba(0, 0, 255, 1)">const</span> {channelList} = useSelector(state=><span style="color: rgba(0, 0, 0, 1)">state.channel)
</span><span style="color: rgba(0, 0, 255, 1)">const</span> dispatch =<span style="color: rgba(0, 0, 0, 1)"> useDispatch()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">React组件中修改store中的数据需要借助另一个hook函数 useDispatch ,他的作用是生成提交action对象的dispatch函数</span>
useEffect(()=><span style="color: rgba(0, 0, 0, 1)">{
dispatch(channelAsync()//异步操作生成的就不是一个action对象了 是一个action函数)
},)</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/qinlinkun/p/18059389
頁:
[1]