Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a need to use StyleSheet.create?

Tags:

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?

like image 724
Guy Avatar asked Oct 07 '16 01:10

Guy


People also ask

What is the point of StyleSheet create () in React Native?

create() ​Creates a StyleSheet style reference from the given object.

Which statement is correct with respect to StyleSheet create?

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.

Which method used to create StyleSheet in react?

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.

What is StyleSheet in react?

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 ...


1 Answers

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!

like image 71
David Avatar answered Oct 05 '22 08:10

David