You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the values of the input-selectors are the same as the previous call to the selector, it will return the previously computed value instead of calling the transform function.
React and Redux 性能,框架优化总结
React
利用React Server Render 提高首屏的渲染速度
使用
Reacr.renderToString
,React.renderToStaticMarkup
。请将方法的bind一律置于
constructor
,避免多次bind。请只传递component需要的props ,切勿一股脑的
<Component {...props} />
。不需要传入状态的component写成
const element
的形式,这样能加快这个element的初始渲染速度。dom上设置可被react识别的同级唯一
key
,否则情况可能不会重新渲染。使用`Stateless Functional Component 无状态组件
React 的无状态组件优雅的实现可复用组件的方式。
栗子如下:
使用
pureRender
,避免组件没有意义的渲染,配合immutable,减少渲染。使用react-css-modules,解决了命名混乱,全局污染以及依赖管理的问题,多人协同开发有时候难免会发生样式上的冲突。
有个需要注意的地方,下面的2个顺序如果颠倒,就会出错。
React-router && Webpack
按需加载模块
把这个按需写着这里,本身需要react-router支持,索性就放在这边了。
加载函数:
具体看webpack官方
看不懂看这篇webpack 按需打包
Redux
Data
项目数据扁平化,不扁平化带来的问题:
通过redux 的
combineReducers
可以很好的扁平化数据。如果使用immutable的话整个侵入性非常的强,不仅要修改combineReducers(因为combineReducers实现就是可变的数据),还需要注意获取数据的时候是否是不可变,以免是null。如果使用immutable可以推荐使用redux-immutable。上面这种写法本身也算是一种immutable,但是要求数据层级不能太深。如果数据相对复杂建议使用immutable。
推荐pure-render-decorator方便的来控制组件渲染。
使用immutable的时候数据转换如下:
immutableJs
Immutable 详解及 React 中实践
redux-immutable
seamless-immutable体积更小,兼容相对好。只支持Arrays and Objects。
数据筛选
reselect
每当store发生改变的时候,connect就会触发重新计算,为了减少重复的不必要计算,减少大型项目的性能开支,需要对selector函数做缓存。推荐使用reactjs/reselect, 缓存的部分实现代码如下。
If the values of the input-selectors are the same as the previous call to the selector, it will return the previously computed value instead of calling the transform function.
假如state.todos 中todos 提供的数据没有发生改变,就会return之前计算好的结果,这样就可以少去非常多的计算成本。
具体的实现可以去看https://github.com/reactjs/reselect#creating-a-memoized-selector。
具体用法:以下修改reudx官方的的demo
Batched actions
如果我们需要同时发送很多action,比如:
可以减少不必要的计算,推荐用到redux-batched-actions
源码很简单
https://github.com/tshelburne/redux-batched-actions/blob/master/src/index.js#L7
Redux DevTools
开发中使用DevTools,建议使用谷歌的插件,不建议在页面结构中插入DevTools。
![](https://cloud.githubusercontent.com/assets/7957859/18002950/aacb82fc-6b93-11e6-9ae9-609862c18302.png)
redux-devtools-extension
在开发环境以及产品环境中移除devTools,避免不必要性能开销和文件大小。
栗子如下:
注意: 需要在webpack中使用
DefinePlugin
插件。相关
以往收集的文章,博客
阮一峰的redux 入门
ReactJS组件间沟通的一些方法
聊一聊基于Flux的前端系统
React 性能工程
聊一聊基于Flux的前端系统
React性能工程-- 深入研究React性能调试
A Better File Structure For React/Redux Applications
React服务器端渲染实践小结
dva 项目解决方案
Getting Started with Redux
React 实践心得:react-redux 之 connect 方法详解
REACT&REDUX中SCROLL LIST封装实践
redux-axios-middleware axios兼容ie9,提供promise ,很方便。
深入理解 react-router 路由系统
Immutable 详解及 React 中实践
webpack+ react-router 按需加载
大概的整个项目具体的优化就这些,细节的插件和实施大家自己去看文档。后续继续更新,喜欢的朋友star支持一下。
The text was updated successfully, but these errors were encountered: