当前位置: 首页 > wzjs >正文

邯郸网站设计怎么做故宫文创产品设计

邯郸网站设计怎么做,故宫文创产品设计,想开一个外企的网站怎么超做,网站开发经验总结与教训Redux与计数器 配套工具使用React Toolkit 创建 counterStore为React注入storeReact组件使用store中的数据React组件修改store中的数据绑定用户交互效果展示action传参Redux异步状态管理 React中的Redux就像Vue中的Vuex和Pinia一样,都是状态管理工具,通过…

Redux与计数器

  • 配套工具
  • 使用React Toolkit 创建 counterStore
  • 为React注入store
  • React组件使用store中的数据
  • React组件修改store中的数据
  • 绑定用户交互
  • 效果展示
  • action传参
  • Redux异步状态管理

  • React中的Redux就像Vue中的Vuex和Pinia一样,都是状态管理工具,通过这种方式可以很方便的实现各个组件中的通信。
  • 下面的代码是通过Redux实现一个计数器

配套工具

  • 在React中使用Redux,官方要求安装两个插件:Redux Toolkitreact-redux

使用React Toolkit 创建 counterStore

import { createSlice } from "@reduxjs/toolkit";const counterStore = createSlice({//name属性不可少,否则会报错name:'counter',//初始化stateinitialState:{count:0},//修改状态的方法 可以直接修改reducers:{//点击加号increment(state){state.count++},//点击减号decrement(state){state.count--}}
})
//解构函数
const {increment,decrement} = counterStore.actions
//获取reducer
const reducer = counterStore.reducer
//按需导出函数
export {increment,decrement}
//默认导出方式导出reducer
export default reducer

为React注入store

  • react-redux负责把Redux和React连接起来,内置Provider组件通过store参数把创建好的store实例注入到应用中,连接正式建立
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import store from './store';
import { Provider } from 'react-redux';//把APP根组件渲染到id位root的dom节点上
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<React.StrictMode><Provider store={store}><App /></Provider></React.StrictMode>
);

React组件使用store中的数据

  • 在React组件中使用store中的数据,需要用到一个钩子函数useSelector,它的作用是把store中的数据映射到组件中
    const {count} = useSelector(state=>state.counter)

React组件修改store中的数据

  • React组件中修改store中的数据需要借助另外一个hook函数 - useDispatch,它的作用是生成提交action对象的dispatch函数
    action传参
  • 在reducers的同步修改方法中添加action对象参数,在调用actionCreater的时候传递参数,参数会被传递到action对象payload属性上
    Redux异步状态管理
  • 步骤:
    • 创建store的写法保持不变,配置好同步修改状态的方法
    • 单独封装一个函数,在函数内部return一个新函数,在新函数中:
      • 封装异步请求获取数据
      • 调用同步actionCreater传入异步数据生成一个action对象,并使用dispatch提交
    • 组件中dispatch的写法保持不变

绑定用户交互

  • 获取状态:通过useSelector,组件从Redux store中获取count状态。这里的state.counter假设是store中的一个对象,其中包含count属性。
  • 获取dispatch函数:通过useDispatch,组件获取到dispatch函数,这个函数用于将action发送到Redux store。
  • 渲染UI:组件渲染两个按钮和当前的count值。点击按钮时,会触发相应的action(increment或decrement),通过dispatch函数发送到Redux store,从而更新状态。
import { useSelector,useDispatch } from "react-redux";
//导入actionCreator
import { increment,decrement,addToNum } from "./store/modules/counterStore";
const App = () => {const {count} = useSelector(state=>state.counter)// const {channelList} = useSelector(state=>state.channel)const dispatch = useDispatch()return (<div><button onClick={()=>dispatch(decrement())}>-</button>{count}<button onClick={()=>dispatch(increment())}>+</button></div>  );
};export default App;

效果展示

在这里插入图片描述

action传参

  • 在reducers的同步修改方法中添加action对象参数,在调用actionCreater的时候传递参数,参数会被传递到action对象payload属性上

Redux异步状态管理

  • 步骤:
    • 创建store的写法保持不变,配置好同步修改状态的方法
    • 单独封装一个函数,在函数内部return一个新函数,在新函数中:
      • 封装异步请求获取数据
      • 调用同步actionCreater传入异步数据生成一个action对象,并使用dispatch提交
    • 组件中dispatch的写法保持不变

文章转载自:

http://eVAoVbhj.Lfdmf.cn
http://EHmUCz62.Lfdmf.cn
http://5LwdtvUo.Lfdmf.cn
http://J8MT55So.Lfdmf.cn
http://YviNGVNT.Lfdmf.cn
http://8mZJLb2A.Lfdmf.cn
http://2AfsSxbx.Lfdmf.cn
http://t8XpjQI6.Lfdmf.cn
http://kA1egqmK.Lfdmf.cn
http://8r13lqGl.Lfdmf.cn
http://csWNqo0D.Lfdmf.cn
http://w99juhM4.Lfdmf.cn
http://EPzMihoT.Lfdmf.cn
http://h27ed7X5.Lfdmf.cn
http://fw3nByYP.Lfdmf.cn
http://hjZ84MOH.Lfdmf.cn
http://lD4ZOFAs.Lfdmf.cn
http://tg2ceOjS.Lfdmf.cn
http://ZvV63Tic.Lfdmf.cn
http://78WsWxdW.Lfdmf.cn
http://rDYdF9ya.Lfdmf.cn
http://3VKI5Rul.Lfdmf.cn
http://FUf9DZU7.Lfdmf.cn
http://Wui7tbOZ.Lfdmf.cn
http://POrZ0PDe.Lfdmf.cn
http://VU5rgM6n.Lfdmf.cn
http://72qp8OQG.Lfdmf.cn
http://EiwMmRVD.Lfdmf.cn
http://vWCoGBoG.Lfdmf.cn
http://MLLpaO9p.Lfdmf.cn
http://www.dtcms.com/wzjs/722377.html

相关文章:

  • 网站建设佰首选金手指十四网站建设的一般步骤
  • 成都专业的网站建设制作公司哪家好北京 外贸网站建设
  • dw软件网站建设教程视频谷歌网站推广排名工具
  • 做美妆网站名称百度热搜榜
  • 网站需要备案吗网站建设千套素材
  • ps 制作网站制作一个自适应网站
  • 龙岩天宫山电话郴州seo网络优化
  • 蓬安网站建设长春网络推广长春seo公司
  • 建设的网站提示危险网站
  • 什么网站专做衣服网站页面关键词优化
  • 佛山专业做网站公司安卓开发培训机构
  • 建设仿优酷视频网站wordpress实例网址
  • 机械设备网站建设网站制作怎么学
  • 湖北建设网站郑州电力高等专科学校宿舍
  • 5g站长工具查询网站设计公司建设网站
  • 网站收录突然减少satellite7 wordpress
  • 新网站建设风格怎么做网站登陆战
  • 垂直行业门户网站什么样建网站
  • 网站费计入什么科目做网站开发学什么
  • 做网站计入什么科目网址你会感谢我的
  • 成都网站网页设计互联网公司排名1000
  • wordpress如何增加page样式seo怎么做整站排名
  • 创意 国外 网站网站 工作室
  • 人设生成器网站制作公司网页需求表
  • 域名解析网站打不开微网站 免费模板
  • lol做视频那个网站好网络营销权威概念是
  • 开发一个网站要多少钱网页动画
  • 同声传译公司网站建设PHP网站新闻发布怎么做
  • 承德网站建设开发wordpress主动推送
  • 华为云怎么做网站如何制作漂亮的微信公众号