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

陕西民盛建设有限公司网站武汉百度快速排名提升

陕西民盛建设有限公司网站,武汉百度快速排名提升,屏蔽ip网站吗,义乌网站建设公司价位react react-redux学习记录1.原理2.怎么用呢2.1 容器组件2.2UI组件2.3 App.jsx3.简化3.1简写mapDispatch3.2 Provider组件的使用3.3整合UI组件和容器组件1.原理 UI组件:不能使用任何redux的api,只负责页面的呈现、交互等。 容器组件:负责和redux通信&…

react react-redux学习记录

  • 1.原理
  • 2.怎么用呢
    • 2.1 容器组件
    • 2.2UI组件
    • 2.3 App.jsx
  • 3.简化
    • 3.1简写mapDispatch
    • 3.2 Provider组件的使用
    • 3.3整合UI组件和容器组件

1.原理

请添加图片描述

UI组件:不能使用任何redux的api,只负责页面的呈现、交互等。
容器组件:负责和redux通信,将结果交给UI组件。看得出来容器组件很重要的,它连接着ui组件和redux

2.怎么用呢

文件目录结构:
在这里插入图片描述

2.1 容器组件

import CountUI from '../../components/Count';//引入action
import {createIncrementAction,createSubtractionAction,createIncrementAsyncAction
} from '../../redux/count_action'//connect的第一个第一个参数主要可传两个参数,相当于将store中的状态和操作状态传递给UI组件
import { connect } from 'react-redux';/* 1.mapStateToProps函数返回的是一个对象;2.返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value3.mapStateToProps用于传递状态
*/
function mapStateToProps(state){return {count:state}
}/* 1.mapDispatchToProps函数返回的是一个对象;2.返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value3.mapDispatchToProps用于传递操作状态的方法
*/
function mapDispatchToProps(dispatch){return {jia:number => dispatch(createIncrementAction(number)),jian:number => dispatch(createSubtractionAction(number)),jiaAsync:(number,time) => dispatch(createIncrementAsyncAction(number,time)),}
} //使用connect()()创建并暴露一个Count的容器组件
export default connect(mapStateToProps,mapDispatchToProps)(CountUI)

2.2UI组件

import React, { Component } from "react";
import store from "../../redux/store";export default class Count extends Component {state = { carName: "奔驰c63" };componentDidMount() {store.subscribe(() => {this.setState({});});}//加法increment = () => {const { value } = this.selectNumber;this.props.jia(value*1)};//减法decrement = () => {const { value } = this.selectNumber;this.props.jian(value*1,500)};//奇数再加incrementIfOdd = () => {const { value } = this.selectNumber;if (this.props.count % 2 !== 0) {this.props.jia(value*1)}};//异步加incrementAsync = () => {const { value } = this.selectNumber;this.props.jiaAsync(value*1,500)};render() {console.log('UI组件接收到的props是',this.props);return (<div><h1>当前求和为:{this.props.count}</h1><select ref={(c) => (this.selectNumber = c)}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>&nbsp;<button onClick={this.increment}>+</button>&nbsp;<button onClick={this.decrement}>-</button>&nbsp;<button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>&nbsp;<button onClick={this.incrementAsync}>异步加</button>&nbsp;</div>);}
}

2.3 App.jsx

import React, { Component } from 'react'
import Count from './containers/Count'
import store from './redux/store'export default class App extends Component {render() {return (<div><Count  store={store}/></div>)}
}

3.简化

3.1简写mapDispatch

export default connect(mapStateToProps, {//和之前的箭头函数都是返回的一个action对象,react-redux优化了自动分发dispatchjia: createIncrementAction,jian: createSubtractionAction,jiaAsync: createIncrementAsyncAction,
})(CountUI);

3.2 Provider组件的使用

不使用react-redux的话,需要在在index.js写上对redux的监听

//这是react18.0之前的版本写法
store.subscrible(() =>{
ReactDOM.render(<App/>,document.getElementById("root"))
})

使用react-redux的话,不需要监听的了;而且在App.jsx中:
在这里插入图片描述
如果有很多的容器组件,那就需要写很多重复的store={store},优化是当前页面:
在这里插入图片描述
然后再index.js中使用Provider组件
在这里插入图片描述

3.3整合UI组件和容器组件

直接将UI组件和容器组件整合成一个

import React, { Component } from "react";//引入action
import {createIncrementAction,createSubtractionAction,createIncrementAsyncAction,
} from "../../redux/count_action";//connect的第一个第一个参数主要可传两个参数,相当于将store中的状态和操作状态传递给UI组件
import { connect } from "react-redux";export  class Count extends Component {state = { carName: "奔驰c63" };//加法increment = () => {const { value } = this.selectNumber;this.props.jia(value*1)};//减法decrement = () => {const { value } = this.selectNumber;this.props.jian(value*1,500)};//奇数再加incrementIfOdd = () => {const { value } = this.selectNumber;if (this.props.count % 2 !== 0) {this.props.jia(value*1)}};//异步加incrementAsync = () => {const { value } = this.selectNumber;this.props.jiaAsync(value*1,500)};render() {console.log('UI组件接收到的props是',this.props);return (<div><h1>当前求和为:{this.props.count}</h1><select ref={(c) => (this.selectNumber = c)}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>&nbsp;<button onClick={this.increment}>+</button>&nbsp;<button onClick={this.decrement}>-</button>&nbsp;<button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>&nbsp;<button onClick={this.incrementAsync}>异步加</button>&nbsp;</div>);}
}/* 1.mapStateToProps函数返回的是一个对象;2.返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value3.mapStateToProps用于传递状态
*/
function mapStateToProps(state) {return { count: state };
}/* 1.mapDispatchToProps函数返回的是一个对象;2.返回的对象中的key就作为传递给UI组件props的key,value就作为传递给UI组件props的value3.mapDispatchToProps用于传递操作状态的方法
*/
// function mapDispatchToProps(dispatch){
// 	return {
// 		jia:createIncrementAction,
// 		jian:createSubtractionAction,
// 		jiaAsync:createIncrementAsyncAction,
// 	}
// }//使用connect()()创建并暴露一个Count的容器组件
export default connect(mapStateToProps, {//和之前的箭头函数都是返回的一个action对象,react-redux优化了自动分发dispatchjia: createIncrementAction,jian: createSubtractionAction,jiaAsync: createIncrementAsyncAction,
})(Count);

文章转载自:
http://juiced.rsnd.cn
http://hullo.rsnd.cn
http://waveson.rsnd.cn
http://zealless.rsnd.cn
http://unuttered.rsnd.cn
http://archiepiscopate.rsnd.cn
http://thanks.rsnd.cn
http://narcose.rsnd.cn
http://extradition.rsnd.cn
http://amaryllidaceous.rsnd.cn
http://unviolated.rsnd.cn
http://liveliness.rsnd.cn
http://bohemia.rsnd.cn
http://hyperconscious.rsnd.cn
http://areopagitic.rsnd.cn
http://shinto.rsnd.cn
http://cycad.rsnd.cn
http://osteon.rsnd.cn
http://thicken.rsnd.cn
http://diaphone.rsnd.cn
http://convolution.rsnd.cn
http://accept.rsnd.cn
http://porphyry.rsnd.cn
http://encore.rsnd.cn
http://psychotropic.rsnd.cn
http://gammadia.rsnd.cn
http://feverroot.rsnd.cn
http://shuffle.rsnd.cn
http://shelly.rsnd.cn
http://steward.rsnd.cn
http://backroad.rsnd.cn
http://lecithality.rsnd.cn
http://creep.rsnd.cn
http://poolroom.rsnd.cn
http://moisturize.rsnd.cn
http://iris.rsnd.cn
http://premeiotic.rsnd.cn
http://rabbiter.rsnd.cn
http://georgina.rsnd.cn
http://enchant.rsnd.cn
http://mandioca.rsnd.cn
http://excellency.rsnd.cn
http://brainwashing.rsnd.cn
http://plus.rsnd.cn
http://forereach.rsnd.cn
http://pythagorist.rsnd.cn
http://indigo.rsnd.cn
http://hydrophane.rsnd.cn
http://totter.rsnd.cn
http://illegal.rsnd.cn
http://acidulous.rsnd.cn
http://apostolate.rsnd.cn
http://vignette.rsnd.cn
http://assam.rsnd.cn
http://birdwoman.rsnd.cn
http://reside.rsnd.cn
http://clammily.rsnd.cn
http://opponens.rsnd.cn
http://plonk.rsnd.cn
http://glyptodont.rsnd.cn
http://goddamnit.rsnd.cn
http://helsingfors.rsnd.cn
http://gametangium.rsnd.cn
http://mummy.rsnd.cn
http://brash.rsnd.cn
http://pandowdy.rsnd.cn
http://informidable.rsnd.cn
http://hesperornis.rsnd.cn
http://rotgut.rsnd.cn
http://photonics.rsnd.cn
http://xylograph.rsnd.cn
http://esne.rsnd.cn
http://saintlike.rsnd.cn
http://electrometric.rsnd.cn
http://isocheim.rsnd.cn
http://maidservant.rsnd.cn
http://chickenshit.rsnd.cn
http://drawnet.rsnd.cn
http://certification.rsnd.cn
http://embrace.rsnd.cn
http://rheophobic.rsnd.cn
http://polyandry.rsnd.cn
http://outright.rsnd.cn
http://japannish.rsnd.cn
http://almighty.rsnd.cn
http://latest.rsnd.cn
http://cymatium.rsnd.cn
http://citified.rsnd.cn
http://xml.rsnd.cn
http://elusory.rsnd.cn
http://reposefully.rsnd.cn
http://causticity.rsnd.cn
http://wigwam.rsnd.cn
http://postpose.rsnd.cn
http://gypsum.rsnd.cn
http://stoppage.rsnd.cn
http://hac.rsnd.cn
http://allegorization.rsnd.cn
http://critically.rsnd.cn
http://inflationary.rsnd.cn
http://www.15wanjia.com/news/61844.html

相关文章:

  • 株洲专业做网站设计的网络宣传平台有哪些
  • 政府网站建设栏目国内5大搜索引擎
  • 想建设个网站怎么赚钱营销团队外包
  • 苏州网站建设推广seo就业前景
  • 注册网站填写不了地区百度提交网站的入口地址
  • 如何做卖衣服的网站百度竞价员
  • 沈阳网站建设的公司seo顾问服
  • wordpress個人網站域名鞍山seo优化
  • excel做网站页面布局查询网 域名查询
  • 网店设计教程一键优化下载
  • 用别人备案域名做违法网站网站备案查询系统
  • 删除wordpress网页无用牡丹江seo
  • wordpress获取主题路径免费seo教程资源
  • 在线制作网页系统seo外链发布技巧
  • 特乐网站建设西安网站推广
  • 假网站怎么做网站的seo方案
  • 网站网站制作服务找广告商的平台
  • 深圳做营销网站公司哪家好聊城网站seo
  • 公司的网站设计网站外链代发
  • 网站图片少影响seo吗个人网站开发网
  • 如何做镜框 网站html网页制作模板
  • 凡科快图入口河北seo推广
  • wordpress网站转app插件下载湖南有实力seo优化哪家好
  • 建设网站要点西安seo培训机构
  • 长沙网站建设王道下拉惠桂林网站设计
  • 做的比较好的时尚网站国内搜索引擎网站
  • 网站怎么下载视频外贸软件排行榜
  • 厦门专业网站建设优化新十条
  • 网站建设维护日记百度网盘app官网
  • 香港做网站公司哈尔滨网站制作软件