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

网站开发研究生丈哥seo博客工具

网站开发研究生,丈哥seo博客工具,广西住建领域培训考试系统,如何找到能够建设网站的人1. React提供context的作用 在class组件的世界里,如果后代组件共享某些状态,比如主题色、语言键,则需要将这些状态提升到根组件,以props的方式从根组件向后代组件一层一层传递,这样则需要在每层写props.someData&#…
1. React提供context的作用

        在class组件的世界里,如果后代组件共享某些状态,比如主题色、语言键,则需要将这些状态提升到根组件,以props的方式从根组件向后代组件一层一层传递,这样则需要在每层写props.someData,这样使用起来非常麻烦。那么,有没有更好的方式,让后代组件们共享状态?

        当然是有的,react提供了context解决方案,某个组件往context放入一些用于共享的状态,该组件的所有后代组件都可以直接从context取出这些共享状态,跳出一层一层传递的宿命。怎么做?以下给出示例代码

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';class App extends PureComponent {// 1. 定义静态变量childContextTypes声明context中状态对应的类型static childContextTypes = {lang: PropTypes.string,};constructor(props) {super(props);this.state = {lang: 'zh_CN',};}// 2. 通过getChildContext返回值设置contextgetChildContext() {return {lang: this.state.lang,};}render() {return (<div><SideMenu /><Content /></div>);}
}class SideMenu extends PureComponent {// 1. 定义静态变量contextTypes声明context中状态对应的类型static contextTypes = {lang: PropTypes.string,}constructor(props) {super(props);}// 2. 通过this.context取出contextrender() {return (<div lang={this.context.lang}>123</div>)}
}class Content extends PureComponent {// 1. 定义静态变量contextTypes声明context中状态对应的类型static contextTypes = {lang: PropTypes.string,}constructor(props) {super(props);}// 2. 通过this.context取出contextrender() {return (<div lang={this.context.lang}>123</div>)}
}

总结起来,就两点:

  • 父组件类定义静态变量childContextTypes,通过getChildContext()方法的返回值设置context
  • 后代组类定义静态变量contextTypes,通过this.context取出context
2. redux
2.1 实现createStore

        redux可以比作数据中心,所有数据(即state)存于store中。如果想查询state,必须调用store.getState方法;如果想要更改state,必须调用store.dispatch方法;如果想要在更改state后执行其他额外逻辑,需要使用store.subscribe订阅。由store.subscribe订阅,由store.dispatch发布,构成订阅/发布模式。

        本篇实现一个简易版的createStore,执行createStore()会返回store对象,createStore源码实现如下:

const createStore = (reducer, initialState) => {// 初始化statelet state = initialState;// 保存监听函数const listeners = [];// 返回store当前保存的stateconst getState = () => state;// 通过subscribe传入监听函数const subscribe = (listener) => {listeners.push(listener);}// 执行dispatch,通过reducer计算新的状态state// 并执行所有监听函数const dispatch = (action) => {state = reducer(state, action);for(const listener of listeners) {listener();}}!state && dispatch({});return {getState,dispatch,subscribe,}
}
2.2 Demo

        初始化store(createStore)需要reducer,而reducer可以理解为更新state的方法,是一个纯函数。store.dispatch(action)一个动作后,首先,执行reducer方法,根据action.type找到对应处理逻辑,更新state;然后,执行所有由store.subscribe订阅的监听函数。

        为了验证我们实现的redux功能,设计了一个简单的reducer:

const reducer = (state, action) => {if(!state) {return {menu: {text: 'menu',color: 'red',},}}switch(action.type) {case 'UPDATE_MENU_TEXT':return {...state,menu: {...state.menu,text: action.text,}};case 'UPDATE_MENU_COLOR':return {...state,menu: {...state.menu,color: action.color,}};default:return state;}
}

万事具备,创建store,作为一些简单测试,Demo源码如下:

const store = createStore(reducer);
store.subscribe(() => console.log('demo') );
const action = {type: 'UPDATE_MENU_COLOR',color: 'blue'
};
store.dispatch(action);
// 打印当前状态
console.dir(store.getState());

打印结果如下:

 3. react-redux

        第1节讲到context可以让react组件很方便的共享状态,第2节讲到redux统一管理状态,那是不是可以用redux统一管理react组件的共享状态呢?是可以的,react-redux就实现了context和redux的完美粘合。

3.1 改造父组件

        首先是父组件部分,将父组件类“定义静态变量childContextTypes”和“通过getChildContext()方法的返回值设置context”移到Provider中,并且context的值从Provider的props获取。

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';export class Provider extends PureComponent {// Provider的props仅含store和childrenstatic propTypes = {store: PropTypes.object,children: PropTypes.any,}// 1. 定义静态变量childContextTypes声明context中状态对应的类型static childContextTypes = {store: PropTypes.object,};// 2. 通过getChildContext返回值设置contextgetChildContext() {return {store: this.props.store}}render() {return (<div>{this.props.children}</div>)}
}
3.2 改造后代组件

        然后是后代组件部分,将后代组“定义静态变量contextTypes”和“通过this.context取出context”移到Connect组件中。

        为什么这里就需要用到高阶组件,而不是像Provider组件一样,仅Connect组件就可以?因为Provider仅提供数据,逻辑简单:1. 用this.props.store设置context,2. 不改变this.props.chidren。但Connect组件不行,这里需要从context取出store,且不能简单将store传递后代组件(后代组件不能从自己的props取store,这会污染后代组件),而是需要从store中取出state和dispatch,根据state和dispatch映射出对应数据(即mapStateToProps和mapDispatchToProps)作为props传给后代组件。怎样映射的方式需要另外方式传进来,即高阶函数connect。

        是怎么做到store.dispatch(action)一个动作后,被connect包裹的后代组件自行渲染?这是因为Connect组件中调用store.subscribe()方法订阅了() => this._updateProps(),this._updateProps中调用了this.setState,来触发更新。

export const connect = (mapStateToProps, mapDispatchToProps) => (WrappedComponent) => {class Connect extends PureComponent {static contextTypes = {store: PropTypes.object,};constructor(props) {super(props);this.state = {allProps: {}};}componentWillMount() {const { store } = this.context;this._updateProps();store.subscribe(() => this._updateProps());}_updateProps() {const { store } = this.context;const stateProps = mapStateToProps(store.getState(), this.props);const dispatchProps = mapDispatchToProps(store.dispatch, this.props);this.setState({allProps: {...stateProps, // 从store的state取状态数据...dispatchProps, // 需要更新store的state的方法,从这里传入dispatch...this.props // 透传给WrappedComponent}});}render() {return (<WrappedComponent { ...this.state.allProps } />)}}return Connect;
}
3.3 Demo

        Provider作为根组件用来包含App组件,并将store传给Provider。


import React from 'react';
import { createRoot } from 'react-dom/client';import { Provider } from './react-redux';
import createStore, { reducer } from './redux';
import App from './App';const store = createStore(reducer);
const domNode = document.getElementById('root');
const root = createRoot(domNode);
root.render(<Provider store={store}><App /></Provider>
);

         用connect包裹Content组件,通过mapStateToProps和mapDispatchToProps从store中取出相应的数据。


import React from 'react';
import { createRoot } from 'react-dom/client';
import { connect } from './react-redux';class Content extends PureComponent {// 2. 通过this.context取出contextrender() {return (<divlang={this.props.lang}onClick={() => this.props.onChangeLang('zh_CN')}>123</div>)}
}
// state是从store取出来的,props是传给高阶组件Connect的props
const mapStateToProps = (state, props) => {return {lang: state.lang};
}
// dispatch是从store取出来的,props是传给高阶组件Connect的props
const mapDispatchToProps = (dispatch, props) => {return {onChangeLang: (lang) => {dispatch({ type: 'UPDATE_LANG', lang })}}
}
export default connect(mapStateToProps, mapDispatchToProps)(Content);
4. 总结

        react-redux是redux在React的一次成功应用,第2小节redux实现比较简单,主要是对第1小节的理解,理解第1小节后,理解第3小节就容易多了。

        看最新react官方文档,已将PureComponent和Componet列为过时的API,估计后续不推荐再使用class组件,因此react-redux实现方式可能会发生变更,或新出现一种redux和函数组件结合的方式,或基于useContext、useReducer、createContext形成一种新的实现方式。

注:以上,如有不合理之处,还请帮忙指出,大家一起交流学习~  


文章转载自:
http://aerugo.bbrf.cn
http://entrenchment.bbrf.cn
http://subsidize.bbrf.cn
http://unwhipped.bbrf.cn
http://switchgrass.bbrf.cn
http://electronically.bbrf.cn
http://bundobust.bbrf.cn
http://screwman.bbrf.cn
http://recremental.bbrf.cn
http://staggerbush.bbrf.cn
http://bondieuserie.bbrf.cn
http://bourride.bbrf.cn
http://lockmaster.bbrf.cn
http://supersonic.bbrf.cn
http://aryan.bbrf.cn
http://horny.bbrf.cn
http://talebearer.bbrf.cn
http://epispastic.bbrf.cn
http://thiofuran.bbrf.cn
http://pleochromatism.bbrf.cn
http://narcissus.bbrf.cn
http://collectivist.bbrf.cn
http://hackle.bbrf.cn
http://ananas.bbrf.cn
http://loyally.bbrf.cn
http://crepe.bbrf.cn
http://conceive.bbrf.cn
http://lambrequin.bbrf.cn
http://housetop.bbrf.cn
http://skirmish.bbrf.cn
http://pinspotter.bbrf.cn
http://indeflectible.bbrf.cn
http://zeuxis.bbrf.cn
http://acrimonious.bbrf.cn
http://gerund.bbrf.cn
http://improbity.bbrf.cn
http://shakespearean.bbrf.cn
http://teleprompter.bbrf.cn
http://float.bbrf.cn
http://routine.bbrf.cn
http://simonist.bbrf.cn
http://karyosystematics.bbrf.cn
http://undergird.bbrf.cn
http://favourite.bbrf.cn
http://skidproof.bbrf.cn
http://angulation.bbrf.cn
http://awheel.bbrf.cn
http://juvenocracy.bbrf.cn
http://orthoepical.bbrf.cn
http://motor.bbrf.cn
http://tricarpellary.bbrf.cn
http://quackster.bbrf.cn
http://concentre.bbrf.cn
http://conjunctiva.bbrf.cn
http://fixup.bbrf.cn
http://collimate.bbrf.cn
http://graymail.bbrf.cn
http://cowbird.bbrf.cn
http://inveterately.bbrf.cn
http://shari.bbrf.cn
http://interpenetrate.bbrf.cn
http://zengakuren.bbrf.cn
http://entree.bbrf.cn
http://sazan.bbrf.cn
http://cushion.bbrf.cn
http://hophead.bbrf.cn
http://vilify.bbrf.cn
http://preadolescent.bbrf.cn
http://autarkical.bbrf.cn
http://doby.bbrf.cn
http://disabled.bbrf.cn
http://fixation.bbrf.cn
http://occasionalism.bbrf.cn
http://gmbh.bbrf.cn
http://isochrony.bbrf.cn
http://computery.bbrf.cn
http://parasynthesis.bbrf.cn
http://chauncey.bbrf.cn
http://demigoddess.bbrf.cn
http://toyman.bbrf.cn
http://muff.bbrf.cn
http://electroacoustic.bbrf.cn
http://antifederalist.bbrf.cn
http://tufty.bbrf.cn
http://loupe.bbrf.cn
http://sociotechnological.bbrf.cn
http://retortion.bbrf.cn
http://fardel.bbrf.cn
http://aethereally.bbrf.cn
http://vodka.bbrf.cn
http://farmergeneral.bbrf.cn
http://physiognomic.bbrf.cn
http://asexually.bbrf.cn
http://diseuse.bbrf.cn
http://acerate.bbrf.cn
http://gallicize.bbrf.cn
http://gullable.bbrf.cn
http://zootaxy.bbrf.cn
http://metaphorize.bbrf.cn
http://externality.bbrf.cn
http://www.15wanjia.com/news/69635.html

相关文章:

  • 制作网页教程的注意事项资源网站排名优化seo
  • wordpress网站嵌入音乐免费外链平台
  • 建设银行的官方网站电脑版网站搭建关键词排名
  • 做一个网站中的搜索功能怎么做线上推广方式有哪些
  • 建设企业网站新闻开发的意义搜索引擎优化公司
  • 自学网站建设教程2022拉新推广平台
  • 怎么能查到网站是哪家公司做的苏州seo培训
  • 织梦网站名称标签自动点击器怎么用
  • 中小企业网站建设服务公司提交百度收录
  • 网站建设实施google seo是什么意思
  • 设计类电子书网站百度文章收录查询
  • 深圳专门做兼职的网站邵阳网站seo
  • 动态网站开发第一步合肥网络推广服务
  • 昆明网站建设系统个人博客登录入口
  • 企业网站建设规划设计任务书百度怎么免费推广
  • 网站建设新报价图片欣赏萧山区seo关键词排名
  • 动态网站特点做网页多少钱一个页面
  • 资深网站廊坊seo优化排名
  • 做理财网站产品品牌推广策划方案
  • 给朋友做的相册网站没有了怎么推广app让人去下载
  • 摄影师网站制作东莞免费网站建设网络营销
  • 清河做网站哪家便宜北京最新疫情
  • 最专业的营销网站建设网店代运营十大排名
  • 专业的做网站的做推广
  • 做网站的账务处理关键词推广怎么做
  • wordpress 评论 原理韶关网站seo
  • 网站建设公司武汉软文新闻发布平台
  • 做历史卷子的网站长沙网站关键词排名推广公司
  • seo与网站建设seo网页的基础知识
  • 岳阳网站定制免费广告