React Native实践react native

浅谈react性能优化的方法

2018-11-15  本文已影响10人  880d91446f17

这篇文章主要介绍了浅谈react性能优化的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

React性能优化思路

Code Splitting

1、例如可以把下面的import方式

`import { add } from` `'./math'``;`
`console.log(add(16, 26));`
`import(``"./math"``).then(math => {`
`console.log(math.add(16, 26));`
`});`

2、例如引用react的高阶组件react-loadable进行动态import。

`import Loadable from` `'react-loadable'``;`
`import Loading from` `'./loading-component'``;`
`const LoadableComponent = Loadable({`
`loader: () => import(``'./my-component'``),`
`loading: Loading,`
`});`
`export` `default` `class App extends React.Component {`
`render() {`
`return` `<LoadableComponent/>;`
`}`
`}`

shouldComponentUpdate避免重复渲染

image
`class CounterButton extends React.Component {`
`constructor(props) {`
`super``(props);`
`this``.state = {count: 1};`
`}`
`shouldComponentUpdate(nextProps, nextState) {`
`if` `(``this``.props.color !== nextProps.color) {`
`return` `true``;`
`}`
`if` `(``this``.state.count !== nextState.count) {`
`return` `true``;`
`}`
`return` `false``;`
`}`
`render() {`
`return` `(`
`<button`
`color={``this``.props.color}`
`onClick={() =>` `this``.setState(state => ({count: state.count + 1}))}>`
`Count: {``this``.state.count}`
`</button>`
`);`
`}`
`}`

附:数据突变(mutated)是指变量的引用没有改变(指针地址未改变),但是引用指向的数据发生了变化(指针指向的数据发生变更)。例如const x = {foo:'foo'}。x.foo='none' 就是一个突变。

使用不可突变数据结构

`class ListOfWords extends React.PureComponent {`
`render() {`
`return` `<div>{``this``.props.words.join(``','``)}</div>;`
`}`
`}`
`class WordAdder extends React.Component {`
`constructor(props) {`
`super``(props);`
`this``.state = {`
`words: [``'marklar'``]`
`};`
`this``.handleClick =` `this``.handleClick.bind(``this``);`
`}`
`handleClick() {`
`// 这段内容将会导致代码不会按照你预期的结果运行`
`const words =` `this``.state.words;`
words.push(``'marklar'``);`
`this``.setState({words: words});`
`}`
`render() {`
`return` `(`
`<div>`
`<button onClick={``this``.handleClick} />`
`<ListOfWords words={``this``.state.words} />`
`</div>`
`);`
`}`
`}`

1、数组使用concat,对象使用Object.assign()

`handleClick() {`
`this``.setState(prevState => ({`
`words: prevState.words.concat([``'marklar'``])`
`}));`
`}`
`// 假设我们有一个叫colormap的对象,下面方法不污染原始对象`
`function` `updateColorMap(colormap) {`
`return` `Object.assign({}, colormap, {right:` `'blue'``});`
`}`

2、ES6支持数组或对象的spread语法

`handleClick() {`
`this``.setState(prevState => ({`
`words: [...prevState.words,` `'marklar'``],`
`}));`
`};`
`function` `updateColorMap(colormap) {`
`return` `{...colormap, right:` `'blue'``};`
`}`

3、使用不可突变数据immutable.js

`const SomeRecord = Immutable.Record({ foo:` `null` `});`
`const x =` `new` `SomeRecord({ foo:` `'bar'` `});`
`const y = x.set(``'foo'``,` `'baz'``);`
`x === y;` `// false`

组件尽可能的进行拆分、解耦

列表类组件优化

`var` `items = sortBy(``this``.state.sortingAlgorithm,` `this``.props.items);`
`return` `items.map(``function``(item){`
`return` `<img src={item.src} />`
`});`
`return` `<img src={item.src} key={item.id} />`

bind函数

绑定this的方式:一般有下面3种方式:

1、constructor绑定
`constructor(props) {`
`super``(props);`
`this``.handleClick =` `this``.handleClick.bind(``this``);` `//构造函数中绑定`
`}`
`//然后可以`
`<p onClick={``this``.handleClick}>`

2、使用时绑定

`<``p` `onClick={this.handleClick.bind(this)}>`

3、使用箭头函数

`<``Test` `click={() => { this.handleClick() }}/>`

不要滥用props

ReactDOMServer进行服务端渲染组件

`// using Express`
`import { renderToString } from` `"react-dom/server"``;`
`import MyPage from` `"./MyPage"``;`
`app.get(``"/"``, (req, res) => {`
`res.write(``"<!DOCTYPE html><html><head><title>My Page</title></head><body>"``);`
`res.write(``"<div id='content'>"``);`
`res.write(renderToString(<MyPage/>));`
`res.write(``"</div></body></html>"``);`
`res.end();`
`});`

客户端使用render方法来生成HTML

`import ReactDOM from` `'react-dom'``;`
`import MyPage from` `"./MyPage"``;`
`ReactDOM.render(<MyPage />, document.getElementById(``'app'``));`

react性能检测工具

image
  • 以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇 下一篇

猜你喜欢

热点阅读