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

做网站钱广州网站制作公司优化

做网站钱,广州网站制作公司优化,珠海网站制作平台,设计网站公司搜索y湖南岚鸿知名Redux 核心 Redux 介绍 Redux 是javaScript 状态容器#xff0c;提供可预测化的状态管理 Redux 工作流程 Actions#xff1a;对象#xff0c;描述对状态进行怎样的操作 Reducer#xff1a;函数#xff0c;操作状态并返回新的状态 Store#xff1a;存储状态的容器提供可预测化的状态管理 Redux 工作流程 Actions对象描述对状态进行怎样的操作 Reducer函数操作状态并返回新的状态 Store存储状态的容器JavaScript对象 View视图HTML页面 React 16 使用 Redux 安装 npm install --save redux创建 store 仓库 在 src 目录下创建一个 store 文件夹,然后在文件夹下创建一个 index.js 文件 import { legacy_createStore as createStore } from redux; import reducer from ./reducer;const store createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ window.__REDUX_DEVTOOLS_EXTENSION__()) // 创建数据存储仓库export default store在 store 文件夹下创建一个 reducer.js 文件 const defaultstate {list: [1,2,3,4,5,6],inpuValue: }export default (state defaultstate) {return state; }在页面中使用 在 src 目录下创建 TodoList.js 页面 在 constructor 引入 this.statestore.getState();使用 this.state.list通过 dispatch 修改里面的值 store.dispatch({type: changeList,value: newList })在 reducer.js 添加对应的方法 const defaultstate {list: [1,2,3,4,5,6],inpuValue: }export default (state defaultstate, action) {switch(action.type) {case changeList:return {...state,list: action.value}default:return state;} }在 constructor 添加 订阅Redux的状态 this.storeChange this.storeChange.bind(this) store.subscribe(this.storeChange) 编写storeChange方法 storeChange(){this.setState(store.getState()) }完整代码 import React, { Component } from react; import store from ./storeclass TodoList extends Component {constructor(props){super(props)this.statestore.getState();this.storeChange this.storeChange.bind(this)store.subscribe(this.storeChange) }handleChange(){this.setState({inputValue:this.inputRef.value})}handleAdd() {let newList this.state.listnewList.push(this.inputRef.value)store.dispatch({type: changeList,value: newList})this.setState({inputValue: })}handledel(index) {const list this.state.listlist.splice(index, 1)store.dispatch({type: changeList,value: list})}storeChange(){this.setState(store.getState())}render() { return ( divdivinput ref{(inputRef){this.inputRefinputRef}} value{this.state.inputValue} onChange{handleChange.bind(this)} /button onClick{handleAdd.bind(this)}新增/button/div{this.state.list.map((item, index) {return (div key{index}p{item}span onClick{() handledel(index).bind(this)} 删除/span/p/div)})}/div);} }export default TodoList;React 18 使用 Redux 安装、创建仓库都与16一样 使用 正常引入用一个变量接收 import store from ./storeconst state store.getState()使用的时候 直接state.xxx 就能使用 const items state.list.map((item, index) {return (div key{index}p{item}span onClick{() handledel(index)} 删除/span/p/div)})修改 一样通过 dispatch store.dispatch({type: changeList,value: list })为了能让页面实时更新必须手动更新 使用 react自带的 useEffect 方法通过 subscribe 监测store更新的函数 useEffect(() {// store.subscribe()是redux提供的监测store更新的函数store.subscribe(() {// 当store数据更新后执行 setUpdate() 组件重新加载实现界面store数据更新setUpdate({})})})const [update,setUpdate] useState({})完整代码 import React, { useRef, useState, startTransition, useEffect } from react; import store from ./storeconst TotoList () {const inputRef useRef()const state store.getState()const [update,setUpdate] useState({})const [value, setValue] useState()const items state.list.map((item, index) {return (div key{index}p{item}span onClick{() handledel(index)} 删除/span/p/div)})const handleChange () {startTransition(() {setValue(inputRef.current.value)})}const handleAdd () {let newList state.listnewList.push(inputRef.current.value)store.dispatch({type: changeList,value: newList})setValue()}const handledel (key) {const list state.listlist.splice(key, 1)store.dispatch({type: changeList,value: list})}useEffect(() {// store.subscribe()是redux提供的监测store更新的函数store.subscribe(() {// 当store数据更新后执行 setUpdate() 组件重新加载实现界面store数据更新setUpdate({})})})return (divdivinput ref{inputRef} value{value} onChange{handleChange} /button onClick{handleAdd}新增/button/div{items}/div) }export default TotoList;
http://www.w-s-a.com/news/929394/

相关文章:

  • 档案室建设网站名贵中药材初加工平台
  • 怎么做优惠券的网站wordpress加载速度
  • 手机网站 分辨率如何创建网站挣钱
  • 网站建设工作标准做模版网站
  • 免费注册微信网站怎样做天猫网站视频
  • 青海建设厅网站通知wordpress如何改文章id
  • 国外搜索网站建设支付网站备案
  • 合肥建站公司有哪家招聘的拼车平台网站开发
  • 网站 备案 固话北京建站模板企业
  • 网站开发的公司wordpress分类目录 模版
  • flashfxp怎么上传对应网站空间wordpress无法创建
  • 建设网站案例分析做网站代理怎么赚钱
  • 唯品会网站建设特色域名备案期间 网站访问
  • 郑东新区建设局网站怎么做万网网站
  • 阿里云上传的网站 服务器路径试用网站开发
  • 做美食原创视频网站网站开发要多钱
  • 怎么做网站作业哪个网站可兼职做logo
  • asp网站搭建教程做网站备案完成之后需要干什么
  • 无锡外贸网站开发兰州网站在哪备案
  • 广州百度网站建设公司天津建设电工证查询网站
  • 网站建设与管理行业发展情况制作网页动态效果
  • wordpress 特色缩略图临沂seo全网营销
  • 隆昌市住房和城乡建设厅网站做网站用什么字体比较好
  • 惠州网站建设设计18款未成年禁用软件ap入口
  • 班级网站 建设目标如何做好网站建设内容的策划书
  • 网站建设与网页设计期末考试清博舆情系统
  • plone网站开发商城网站建设怎么收费
  • 旺旺号查询网站怎么做公司门户网站项目模版
  • 网站免费一站二站四站上海网站怎么备案表
  • 漫画交流网站怎么做开发微信小程序公司