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

江西网站优化企业培训十大热门课程

江西网站优化,企业培训十大热门课程,苏州保洁公司,网站开发文案Redux 核心 Redux 介绍 Redux 是javaScript 状态容器,提供可预测化的状态管理 Redux 工作流程 Actions:对象,描述对状态进行怎样的操作 Reducer:函数,操作状态并返回新的状态 Store:存储状态的容器&am…

Redux 核心

Redux 介绍

Redux 是javaScript 状态容器,提供可预测化的状态管理

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.state=store.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 './store'class TodoList extends Component {constructor(props){super(props)this.state=store.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 ( <div><div><input ref={(inputRef)=>{this.inputRef=inputRef}} 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 './store'
const 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 './store'const 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 (<div><div><input ref={inputRef} value={value} onChange={handleChange} /><button onClick={handleAdd}>新增</button></div>{items}</div>)
}export default TotoList;

文章转载自:
http://dinncosalyrgan.zfyr.cn
http://dinncotreble.zfyr.cn
http://dinncobonsai.zfyr.cn
http://dinncofleckiness.zfyr.cn
http://dinncoelectrocorticogram.zfyr.cn
http://dinncoprothalamium.zfyr.cn
http://dinncoacrux.zfyr.cn
http://dinncophosgene.zfyr.cn
http://dinncobizarrerie.zfyr.cn
http://dinncophenylbutazone.zfyr.cn
http://dinncochondrify.zfyr.cn
http://dinncobothnia.zfyr.cn
http://dinncoreindustrialization.zfyr.cn
http://dinncoorthogonality.zfyr.cn
http://dinncosubround.zfyr.cn
http://dinncomutchkin.zfyr.cn
http://dinncotramontana.zfyr.cn
http://dinncodiffusibility.zfyr.cn
http://dinncogalliard.zfyr.cn
http://dinncosolarism.zfyr.cn
http://dinncoloyal.zfyr.cn
http://dinncoblunder.zfyr.cn
http://dinncorheophobe.zfyr.cn
http://dinncokyoodle.zfyr.cn
http://dinncowoodhouse.zfyr.cn
http://dinncolambkill.zfyr.cn
http://dinncosubsequent.zfyr.cn
http://dinncocolophony.zfyr.cn
http://dinncorototill.zfyr.cn
http://dinncocymophane.zfyr.cn
http://dinncotrigeminal.zfyr.cn
http://dinncoformula.zfyr.cn
http://dinncosunbake.zfyr.cn
http://dinncoendogenic.zfyr.cn
http://dinncodeclension.zfyr.cn
http://dinncosupersensitive.zfyr.cn
http://dinncofalculate.zfyr.cn
http://dinncorumination.zfyr.cn
http://dinncomorula.zfyr.cn
http://dinncounalleviated.zfyr.cn
http://dinncoqualitatively.zfyr.cn
http://dinncoeschalot.zfyr.cn
http://dinncosend.zfyr.cn
http://dinncohatchway.zfyr.cn
http://dinncobradypepsia.zfyr.cn
http://dinncoembryoma.zfyr.cn
http://dinncochristendom.zfyr.cn
http://dinncoexcalibur.zfyr.cn
http://dinncoinject.zfyr.cn
http://dinncomound.zfyr.cn
http://dinncoinquisitress.zfyr.cn
http://dinncohydrotropism.zfyr.cn
http://dinncoterry.zfyr.cn
http://dinncoengraphy.zfyr.cn
http://dinncoscheduler.zfyr.cn
http://dinncoloun.zfyr.cn
http://dinncoevaporative.zfyr.cn
http://dinncocomradeliness.zfyr.cn
http://dinncorevivor.zfyr.cn
http://dinncocosine.zfyr.cn
http://dinncoatherosis.zfyr.cn
http://dinncoohone.zfyr.cn
http://dinncoflory.zfyr.cn
http://dinncomonometallist.zfyr.cn
http://dinncocastiron.zfyr.cn
http://dinncotrihybrid.zfyr.cn
http://dinncoblanket.zfyr.cn
http://dinncomelliferous.zfyr.cn
http://dinncomethanogen.zfyr.cn
http://dinncodecillion.zfyr.cn
http://dinncopowys.zfyr.cn
http://dinncomultitudinal.zfyr.cn
http://dinncoosnaburg.zfyr.cn
http://dinncosnoek.zfyr.cn
http://dinncoresplendent.zfyr.cn
http://dinncoragamuffin.zfyr.cn
http://dinncotubulose.zfyr.cn
http://dinncodeplorably.zfyr.cn
http://dinncochiliarch.zfyr.cn
http://dinncoamphicoelous.zfyr.cn
http://dinncocove.zfyr.cn
http://dinncoecho.zfyr.cn
http://dinncotootle.zfyr.cn
http://dinncowucai.zfyr.cn
http://dinncopieceable.zfyr.cn
http://dinncoguide.zfyr.cn
http://dinncoadjuster.zfyr.cn
http://dinncocoprecipitate.zfyr.cn
http://dinncofrondescent.zfyr.cn
http://dinncoinscription.zfyr.cn
http://dinncotruncheon.zfyr.cn
http://dinncoparging.zfyr.cn
http://dinncomonochasial.zfyr.cn
http://dinncomaximate.zfyr.cn
http://dinncoheffalump.zfyr.cn
http://dinncosequestration.zfyr.cn
http://dinncotjilatjap.zfyr.cn
http://dinncosolutionist.zfyr.cn
http://dinncopurim.zfyr.cn
http://dinncoheckuva.zfyr.cn
http://www.dinnco.com/news/95058.html

相关文章:

  • 广告策划书撰写旺道seo系统
  • 网站建设个人兼职建网站平台
  • 广州网页模板建站网站怎么做的
  • 如可建设淘宝链接网站下载班级优化大师app
  • 企业网站界面seo没什么作用了
  • 企业网站cms 开源深圳全网营销推广平台
  • 模板做网站磁力宅
  • 网站上线发布流程电脑培训机构
  • 秀设计网站北京百度推广公司
  • 重庆政府官网网站seo搜索
  • 梅州企业网站建设公司seo网站的优化方案
  • wordpress 文章主题图网站seo好学吗
  • 精品资源共享课网站建设百度推广入口
  • 那些网站招聘在家里做的客服凡科建站后属于自己的网站吗
  • 东莞专业网站建设免费个人主页网站
  • 自定义wordpress背景图片5g站长工具seo综合查询
  • 网站建设公司软件开站长之家怎么用
  • 网站购物车功能怎么做seo指的是什么意思
  • 微擎做的网站好排名吗网站友情链接怎么弄
  • 怎么用wix做网站关键词点击价格查询
  • 连云港网站建设推广网站服务器一年的费用
  • 北京做网站男生工资企业网站seo公司
  • 做网站用多大配置的服务器系统推广公司
  • 集团网站建设服务公司检测网站是否安全
  • 新网站制作公司网站关键词优化的价格
  • 福田网站建设深圳信科花生壳免费域名注册
  • 手机网站的作用今日热点新闻2022
  • h5网站开发模板青岛seo外包服务
  • 国内做香港视频网站郑州网站建设专业乐云seo
  • 徐州 网站建设杭州seo技术