电商App中ReactNative性能优化小结

一、减少re-render

1、shouldComponentUpdate

 shouldComponentUpdate(nextProps, nextState) {return !(nextProps.districtId === this.props.districtId &&nextProps.currentDestId === this.props.currentDestId &&nextProps.showLoading === this.props.showLoading &&nextState.setReceiveProps === this.state.setReceiveProps)} 

1.可以拿到前后状态的 state/props,然后手动检查状态是否发生了变更,再根据变更情况来决定组件是否需要重新渲染2.必须考虑和此组件相关的所有 props 和 state,如果有遗漏,就有可能出现数据和视图不统一的情况。所以使用的时候一定非常小心。### 2、React memo

1.React.memo高阶组件,参数为组件,返回值为新组件,它与 React.PureComponent 非常相似,但只适用于函数组件,而不适用 class 组件。
2.memo:备忘录,记事本,记忆组件渲染结果,React 将跳过渲染组件的操作并直接复用最近一次渲染的结果。

function Button(props) {return <button color={this.props.color} />;
}
//自定义比较函数
function areEqual(prevProps
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值