Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native inline styles and performance

Does the following:

<Text style={{color: 'blue', fontSize: 30}} /> 

Have any performance implications compared to:

<Text style={styles.blueButton} />  ...  const styles = StyleSheet.create({   blueButton: {     color: 'blue',     fontSize: 30,   } }); 
like image 865
Martin Konicek Avatar asked Sep 05 '16 18:09

Martin Konicek


People also ask

Does inline style affect performance?

Ultimately, it's important in terms of inlining CSS and performance that you don't just dump all the CSS for your site into the head. If you inline too much, the performance implications will be worse than what you started with.

Are inline styles GOOD FOR React?

Rule of Thumb. The official React documentation frowns upon the use of inline styling as a primary means of styling projects and recommends the use of the className attribute instead.

Is inline styling faster?

The main difference between inline CSS and external CSS is that inline CSS is processed faster as it only requires the browser to download 1 file while using external CSS will require downloading HTML and CSS files separately.

Is inline styling slow?

Yes, inline styles are technically faster than an external stylesheet because you are making one less request on top of the page but using an external stylesheet is much preferred for code maintainability.


1 Answers

From the docs for StyleSheet

Performance:

  • Making a stylesheet from a style object makes it possible to refer to it by ID instead of creating a new style object every time.
  • It also allows to send the style only once through the bridge. All subsequent uses are going to refer an id (not implemented yet).

Another benefit is that style errors will be generated at compile time as opposed to run time.

I personally still like using inline styles (and creating new components for shared styles) because it makes the code more readable for me and the performance hit has not been noticeable.

like image 139
FuzzyTree Avatar answered Sep 21 '22 03:09

FuzzyTree