I am defining styles in this manner:
const styles = StyleSheet.create({
container: {},
tabContent: {
alignItems: "flex-start",
flexDirection: "column",
padding: 21
},
tabHeader: {
flex: 0,
fontSize: 10,
paddingTop: 10,
},
tabText: {
flex: 0,
fontSize: 14,
paddingTop: 10,
textAlign: "left"
},
})
However, one time I forgot to use StyleSheet.create and used a plain object:
const styles = {
container: {},
tabContent: {
alignItems: "flex-start",
flexDirection: "column",
padding: 21
},
tabHeader: {
flex: 0,
fontSize: 20,
paddingTop: 10,
},
tabText: {
flex: 0,
fontSize: 14,
paddingTop: 10,
textAlign: "left"
},
}
Surprisingly it worked. Is there a need to use StyleSheet.create to define styles or can one use plain objects?
create() Creates a StyleSheet style reference from the given object.
Question: Web development questions with React, native and jsa) Which statement is CORRECT with respect to 'StyleSheet. create'?- This method ensures that the style values create are immutable and opaque and they are only created once- Stylesheet.
If you wanted to get the styles out of the created styles object, you should have used the flatten method. The majority of the answers reference this flatten method and you could not access styles property as if it was a normal object. E.g. const styles = StyleSheet.
React Native StyleSheet is a way of styling an application using JavaScript code, the main function of react native StyleSheet is concerned with styling and structuring of components in an application, all components of react native make use of a prop known as style, names and properties values work in a similar way as ...
I think its still better to use StyleSheet
where possible as its more performant than using regular objects.
From React Native Docs:
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).
Remember seeing something somewhere (think it was a github issue) with a similar question and someone saying it's still better to use StyleSheet
for performance.
Hope this helps!
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