初见

React Native Performance Improve

2019-10-31  本文已影响0人  果维

1. Reduce Redundant Re-Render

因为公司要求的原因,所以本文档使用英文编写,因本人英文水平有限,如有错误,请及时指正,谢谢!

1.1 Use PureComponent / React.memo()

const MyComponent = React.memo(function MyComponent(props) {
         /* only re-renders if props change */ 
});

1.2 Use shouldComponentUpdate

2. Modularization

3. requestAnimationFrame / InteractionManager.runAfterInteractions

scrollToEnd can be problematic without requestAnimationFrame.

componentDidUpdate(prevProps , prevState) {  
// this._scrollViewRef && this._scrollViewRef.scrollToEnd({ animated: false }); 
  requestAnimationFrame(() => this._scrollViewRef && this._scrollViewRef.scrollToEnd({ animated: false }));   
} }

4. The optimization of FlatList / Partial Loading LargeList

5. Data flattening

6. Use attributes key wisely

① In an array of components generated by method of map or other operation, each item component sets the key attribute. This is beneficial for RN to identify subcomponents and improve the reusability of components.

② When you need a component rebuild. For example, for a large list of search function, to make the list after each search to scroll to the top, similar scrollToTop method experience is not good, so we can give our FlatList set a key properties, such as the state of searchText as the key, so the content changes of each search, FlatList will be regenerated, and regenerate the data, the default is at the top.

7. Other ways

// The wrong way to write it
<Button onPress={() => {}}/>

// The correct way to write it
<Button onPress={this.onChange}/>
onChange =()=>{}
//In some places
this._throttle = throttle(this.func ,100);

//release throttle When the interface is destroyed
componentWillUnmount = () => {
    this._throttle.cancel();
}
if (!global.__DEV__) {
  global.console = {
    info: () => {},
    log: () => {},
    warn: () => {},
    debug: () => {},
    error: () => {},
    //Don't forget. It may appear in third-party plugins.
    assert: () => {},
  };
} 

8. Redux Improvement

9. Routing / Navigation

上一篇 下一篇

猜你喜欢

热点阅读