React Native Performance Optimisation With Hooks
Tags: react, reactnative, javascript, codequality
Published: 2020-06-12 23:39:48



## Working Just fine but not that great? :expressionless:.
Its very easy for developers to write working `react-native` app, but not best performing react-native app. Its been a while, i was finding a way to write a react-native app with maximum performance possible. I was trying all the best practice possible to make app better. 
Here are some of my collections of methods, tips & tricks to improve react-native app performance:fire:.
## 1. Stop using inline functions :cyclone:
First thing, stop using inline function calls right now, react native thinks that the props which has inline function is a new value every-time & it causes unnecessary re-renders in its children components.
#### Solution
 Move all your inline functions to const. (& next step as well )
#### Example:
**Before**

{%gist https://gist.github.com/ltsharma/beeac1b170695e40f5265ec077d0cb06.js %}

**After:white_check_mark:**
{% gist https://gist.github.com/ltsharma/b22aa8c7507e144477c3eeba7d118685 %}
So now app performance is far better?. humm!, not yet, but we are doing some progress. for now we made component much legible.
## 2. Make proper use of `useCallback`:recycle:.
Any of your functions which uses to re-render your component, 
which should not cause unnecessary re render, whenever you don't want, whichever the functions used inside jsx elements, bring it out like shown in last step and apply our hero hook `useCallback`
what will it do?. that is not part of this post anyway, you can find in in many docs & posts. But i will just show how we can implement it to improve react-native performance.
#### Example
{% gist https://gist.github.com/ltsharma/1be2903c9cfa0e6b0003c454f7b18bfb %}
And now we defiantly made some progress by applying our hero hook. This makes sure the FlatList will not re-render because the `AwesomeChild` is memorised & there is nothing to change its value.
 you can observe from above code, empty bracket is used just after our old function wrapping with useCallback, that is dependancy, that is required incase the function is consuming any of other values.
#### Example
{% gist https://gist.github.com/ltsharma/d5152ca1378fed90e651c5fca811b5cc %}
The above code is written to add 1 to its last state, but it always sets 1, because the useCallback memorised the states `pressCount` as 0 at the first render, whenever we use the state inside useCallback it is always 0, so every time we press, it will be 0+1 = 1. to get the latest values, we need to add states to the empty array inside useCallback. i.e, `useCallback(()=>{...},[pressCount])` 
So its bit of headache to find dependency and fill, i know right!?, for that you just need to configure eslint & eslint-react-hook, after that VS code will take care of it.
**Before applying `useCallback`**
![Before applying useCallback](https://dev-to-uploads.s3.amazonaws.com/i/m1p1woxdyqnujo82b1k3.gif).
Notice the lag of activating tab.
**After applying `useCallback`**
![After applying useCallback](https://dev-to-uploads.s3.amazonaws.com/i/akb5e1b8ssaowxzax8c0.gif).

## 3. `memo` for Whole component:vertical_traffic_light:.
use `export default React.memo(AwesomeChild)` to export almost all of your component, this is very similar to PureComponent from react class turf. it prevents re-rendering of components by comparing prev & next props, sometime it will allow render for some of unwanted prop changes, to level up the restrictions, we can use areEqual function as 2nd argument of the `React.memo` function,
#### Example 
Unrestricted memo
{% gist https://gist.github.com/ltsharma/2c97a0322f9c05f2d17db93cca1f3b56 %}
 Restricted memo
 {% gist https://gist.github.com/ltsharma/270ac708d7e1a32b6c377d7776760061 %}
In this, component will re-render only if `text` prop changes from parent component, no re-renders if `style` prop changes.
(in most of the cases, normal memo will work without any problems)

## 4. Others tips:wrench:.
- configure VS Codes <-> eslint for writing optimum codes & use prettifier for make your code neat & clean.
- Keep imports clean, remove unused imports, (eslint will help you by showing it).
- Don't use inline styles, make maximum use of StyleSheet, (eslint will help you by showing it). 
- carefully use `useEffect`, with proper dependency and conditions to do the actions.
- to find-out what is causing waste re render - use this nice package, install as dev dependancy https://github.com/welldone-software/why-did-you-render.
- My .eslint.js: https://gist.github.com/ltsharma/cc88c8f96536f656570b23586b4e3098
- My devdependancy: https://gist.github.com/ltsharma/04344dbb530d65eb1341b415e5df5962

I Hope this post helps some of you guys, i figured it out by spending hours together, thought of sharing which is not easily available on internet. 
Comment your thought & tips...