8. StyleSheet #

Created Wed Nov 15, 2023 at 1:08 AM

import { StyleSheet } from 'react-native';

const myStyles = StyleSheet.create({ 
	mySection: { marginTop: 24, padding: 16 },
	myHeading: { color: "blue", fontSize: 48 }
});

function MyComponent() {
	return <View>
		<View style={myStyles.mySection}>
		  <Text style={myStyles.myHeading}>Heading 1</Text>
		</View>
		
		<View style={myStyles.mySection}>
		  <Text style={myStyles.myHeading}>Heading 2</Text>
		</View>
	</View>;
}

Why use StyleSheet #

  1. Reuse aka DRY
  2. For style validation checks: 2nd level keys inside StyleSheet config are checked by RN, for typos.
  3. Leaves scope for internal RN optimization, if it’s implemented in the library later.