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🔥.
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.
Move all your inline functions to const. (& next step as well )
So now app performance is far better?. humm!, not yet, but we are doing some progress. for now we made component much legible.
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
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.
And now we defiantly made some progress by applying our hero hook. This makes sure the FlatList will not re-render because the
AwesomeChildis 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.
The above code is written to add 1 to its last state, but it always sets 1, because the useCallback memorised the states
pressCountas 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,
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.
Notice the lag of activating tab.
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
In this, component will re-render only if
textprop changes from parent component, no re-renders if
(in most of the cases, normal memo will work without any problems)
- 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...