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, } });
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With