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

林芝市网站建设_网站建设公司_网站制作_seo优化

天津河北做网站的公司排名,做装修网站,建立读音,企业网站建设最需要的是什么问题描述: 在某个组件中.有可能频繁的取数据(但是数据未改变,因此不需要更新).数据的频繁请求会触发render函数,造成性能消耗模拟代码如下 export class CommentList extends Component {constructor(props) {super(props);this.state {comments: []}}// 模拟频繁的获取新数…问题描述: 在某个组件中.有可能频繁的取数据(但是数据未改变,因此不需要更新).数据的频繁请求会触发render函数,造成性能消耗模拟代码如下 export class CommentList extends Component {constructor(props) {super(props);this.state {comments: []}}// 模拟频繁的获取新数据componentDidMount() {setInterval(() {this.setState({comments:[{ body: 奇怪的栗子, author: odd marron},{ body: 好吃的栗子, author: nice marron}]})}, 1000)}render() {return (div{this.state.comments.map((c,i)(Comment key{i} data{c} /))}/div)} }class Comment extends React.Component{console.log(render comment);render(){return (divp {this.props.data.body} /pp --- {this.props.data.author} /p/div)} }可以看到,数据在未改变时,频繁的调用render 解决方案1 React 15.3之前(无PureComponent)使用shouldComponentUpdate在shouldComponentUpdate中判断当前body是否和传入的数据相等. class Comment extends React.Component{shouldComponentUpdate(nextProps) {if(nextProps.data.body this.props.data.body nextProps.data.author this.props.data.author) {return false;}return true;}render() {return (divp {data.body} /pp --- {data.author} /p/div);} }解决方案2 PureComponent解决方案对上面代码修改后如下 import React, { Component } from react;// 容器组件 export class CommentList extends Component {constructor(props) {super(props);this.state {comments: []};}componentDidMount() {setTimeout(() {this.setState({comments: [{ body: 奇怪的栗子 , author: odd marron },{ body: 好吃的栗子, author: nice marron }]});}, 1000)}render() {return (div{this.state.comments.map((c,i) (Comment key{i} {...c} /))}/div);} } // 展示组件 class Comment extends React.PureComponent{render() {console.log(render comment);return (divp{this.props.data.body}/pp--- {this.props.data.author}/p/div)} }此时数据为改变时 不更新. 注意: 使用PureComponent时,其传递的参数只能是基本类型引用或简单的非多层嵌套对象原因见下面PureComponent源码: import shallowEqual from ./shallowEqual import Component from ./Componentexport default function PureComponent(props, context) {Component.call(this, props, context); }PureComponent.prototype Object.create(Component.prototype); PureComponent.prototype.constructor PureComponent; PureComponent.prototype.isPureReactComponent true; PureComponent.prototype.shouldComponentUpdate shallowCompare;function shallowCompare (nextProps, nextState) {return !shallowEqual(this.props, nextProps) ||!shallowEqual(this.state, nextState); }export default function shallowEqual(objA, objB) {// 比较引用地址if(objA objB){return true}// 只检查一层if(typeof objA !object || obja null || typeof objB ! objB || objB null) {return false}var keysA Object.keys(objA);var keysB Object.keys(objB);if(keysA.length ! keysB.length ){return false}for(var i 0; i keysA.length; i ){if(!objB.hasOwnproperty(keysA[i]) || objA[keysA[i]] ! objB[krysA[i]] ){return false;}}return true; }PureComponent 对shouldComponentUpdate进行设置.比较引用地址然后比较第一层…因此使用PureComponent时,应注意将对象结构出来使用 解决方案3 React.memo (React v16.6.0以上)React.memo是个高阶函数更改上面Comment const Comment React.memo((props) {console.log(render comment);return (divp{props.body}/pp --- {props.author}/p/div) });
http://www.ihoyoo.com/news/63976.html

相关文章:

  • 零基础网站开发设计山西品牌网站建设
  • WordPress网站关闭插件企业部门网站建设流程
  • 汕尾网站网站建设牛商网招聘
  • 搜索引擎 网站推广 举例电商网站设计方案
  • 相册模版网站图片展示产品朋友圈推广词
  • 环保网站开发定制网站制作服务商
  • 孝感市门户网站管理中心直接买个域名就能自己做网站
  • 网站建设方案总结语wordpress elegant
  • 宝塔搭建网站中国建设银行郑州分行网站
  • 没营业执照怎么做网站注册公司找黄牛一般多少钱
  • 网站图片像素购物网站排版设计
  • 昭平县建设局网站简单网站开发实例教程
  • 媒体网站怎么申请鞍山微信小程序定制开发
  • 建设银行河北招聘网站境外电商是做什么的
  • 东莞高端网站设计陕西省建设网一体化平台
  • 网站 百度认证河南浪博网站开发
  • 个人网站备案转公司备案二级域名对于英文网站推广有什么影响
  • 江门模板建站源码成都网站建设scjsc888
  • 网站建设的注意事项计算机网页设计专业学什么
  • 免费外贸接单网站汕头中企动力
  • 电子商务模拟实训报告企业网站建设wordpress 实时更新
  • 网站数据库管理系统免费推广软件有哪些
  • 建设银行小微企业网站进不了网页制作平台有什么
  • 餐饮公司 网站建设乐装网
  • 自己做的一个网站怎么赚钱中企动力待遇怎么样
  • 景山网站建设网上书城网站开发环境
  • 网页制作软件dw还需要什么seo搜索引擎优化求职简历
  • 企业网站推广排名网站建设 苏州
  • 门店到什么地步可以做网站域名三天更换一次
  • seo教程网站长春网站制作的公司哪家好