What is the point of StyleSheet.create

前端 未结 3 1972
旧时难觅i
旧时难觅i 2020-12-05 17:01

I\'m reading the React Native docs / tutorial, and I\'m wondering what the point of the StyleSheet.create function is.

For example, the tutorial has the

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 17:22

    StyleSheet.create does not add performance gains anymore.

    https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29265#issuecomment-430783289

    quoting the github comment:

    @alloy I understand what docs says, but can prove that code:

    const myStyle: ViewStyle = { flex: 1 } export const FlexView:
    React.SFC = (props) => {props.children}
    

    has almost same performance (even slightly faster) compared to

    const s = StyleSheet.create({ flex: { flex: 1 } }) 
    export const FlexView: React.SFC = (props) => {props.children} 
    

    because if you look at sources, you discover that latest chunk effectively extracted to this (see: https://github.com/facebook/react-native/blob/0.57-stable/Libraries/StyleSheet/StyleSheet.js#L373):

    const s = { flex: { flex: 1 } }
    export const FlexView = (props) => {props.children}
    

    And yes, in previous versions of RN it was global registry of styles, but it was even more slow, because it never crossed bridge border actually (proof from 0.55 branch)

提交回复
热议问题