网站怎么做图片,织梦手机网站制作教程,杭州微网站建设公司哪家好,现在帮别人做网站赚钱不1. 环境搭建
插件安装#xff1a;Redux Toolkit和react-redux
npm i reduxjs/toolkit react-redux2、
store目录结构设计
集中状态管理的部分会单独创建一个store目录#xff08;在src下#xff09;应用通常会有很多个子模块#xff0c;所以还会有个modules目录#x…1. 环境搭建
插件安装Redux Toolkit和react-redux
npm i reduxjs/toolkit react-redux2、
store目录结构设计
集中状态管理的部分会单独创建一个store目录在src下应用通常会有很多个子模块所以还会有个modules目录在内部编写业务分类的子storestore中的入口文件index.js的作用是组合modules中的所有子模块并导出store
3、使用React Toolkit创建counterStore
创建各模块的store counterStore.js
import { createSlice } from reduxjs/toolkit;// 创建store
const counterStore createSlice({name: counter,// 初始化stateinitialState: {count: 0},// 修改状态的方法 reducers: {inscrement(state) {state.count;},decrement(state) {state.count--;}}
})// 结构出actionCreater函数
const {inscrement, decrement} counterStore.actions
// 获取reducer
const reducer counterStore.reducer// 以按需导出的方式导出actionCreater
export {inscrement, decrement}
// 以默认导出的方式导出reducer
export default reducer在index,js中组合store
import { configureStore } from reduxjs/toolkit;
// 导入子模块reducer
import counterReducer from ./modules/counterStoreconst store configureStore({reducer: {counter: counterReducer}
})export default store4、为React注入store
React-redux负责把Redux和redux连接起来内置Provider组件通过store参数把创建好的store实例注入到应用中。
import React from react;
import ReactDOM from react-dom/client;
import ./index.css;
import App from ./App;
import reportWebVitals from ./reportWebVitals;
import store from ./store;
import { Provider } from react-redux;const root ReactDOM.createRoot(document.getElementById(root));
root.render(React.StrictModeProvider store{store}App //Provider/React.StrictMode
);reportWebVitals();5、在React组件中使用store中的数据
使用useSelector钩子函数把store中的数据映射到组件中 import React from react;
import ./App.css;import { useSelector } from react-redux;function App() {// 这里state.counter和store中的reducer.counter是对应的const {count} useSelector(state state.counter);return (React.Fragmentdiv{count}/div/React.Fragment);
}export default App;
6、React组件中修改store中的数据
引入useDispatch钩子函数作用生成提交的action对象的dispatch函数 import React from react;
import ./App.css;import { useDispatch, useSelector } from react-redux;// 倒入actionCreater
import {inscrement, decrement} from ./store/modules/counterStorefunction App() {// 这里state.counter和store中的reducer.counter是对应的const {count} useSelector(state state.counter);const dispatch useDispatch()return (React.Fragmentdiv{/* 调用dispatch提交action对象 */}button onClick{() dispatch(decrement())} - /buttonspan{count}/spanbutton onClick{() dispatch(inscrement())} /button/div/React.Fragment);
}export default App;7、在action中传参
在reducers的同步修改方法中添加action对象参数在调用actionCreator时传递参数参数会被传递到action对象的payload属性上 定义方法通过payload获取传入的参数
import { createSlice } from reduxjs/toolkit;// 创建store
const counterStore createSlice({name: counter,// 初始化stateinitialState: {count: 0},// 修改状态的方法 reducers: {inscrement(state) {state.count;},decrement(state) {state.count--;},addToNum (state, action) {// payload是固定属性state.count action.payload}}
})// 解构出actionCreater函数
const {inscrement, decrement, addToNum} counterStore.actions
// 获取reducer
const reducer counterStore.reducer// 以按需导出的方式导出actionCreater
export {inscrement, decrement, addToNum}
// 以默认导出的方式导出reducer
export default reducer使用 button onClick{() dispatch(addToNum(10))} add To 10/buttonbutton onClick{() dispatch(addToNum(20))} add To 20/button8、异步代码
store写法不变单独封装一个函数返回一个新函数在新函数中封装异步请求获取数据并调用同步actionCreater传入异步数据生成一个action对象并使用dispatch提交 channelStore.js
import { createSlice } from reduxjs/toolkit;
import axios from axios;const channelStore createSlice({name: channel,initialState: {channelList: []},reducers: {setChannels(state, action) {state.channelList action.payload}}
})// 异步请求部分
const {setChannels} channelStore.actionsconst fetchChannelList () {return async (dispatch) {const res await axios.get(http://geek.itheima.net/v1_0/channels)dispatch(setChannels(res.data.data.channels))}
}const reducer channelStore.reducer
export {fetchChannelList}
export default reducer
组件中dispatch的写法保持不变 store/index.js不变
import { configureStore } from reduxjs/toolkit;
// 导入子模块reducer
import counterReducer from ./modules/counterStore
import channelReducer from ./modules/channelStoreconst store configureStore({reducer: {counter: counterReducer,channel: channelReducer}
}) export default store在组件中使用
import React, { useEffect } from react;
import { useDispatch, useSelector } from react-redux;// 倒入actionCreater
import { fetchChannelList } from ./store/modules/channelStore;function App() {const {channelList} useSelector(state state.channel)const dispatch useDispatch()useEffect(() {dispatch(fetchChannelList())}, [])return (React.Fragmentdiv{/* 调用dispatch提交action对象 */}ul{channelList.map(item li key{item.id}{item.name}/li)}/ul/div/React.Fragment);
}export default App;
文章转载自: http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.cwgpl.cn.gov.cn.cwgpl.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn