Lesson 66 of 60 – Typing StyleSheet
100%

Typing StyleSheet

StyleSheet is used to create and organize styles in React Native applications. When using TypeScript, styles can be checked during development, helping us write safer and more maintainable UI code.

Note: React Native provides built-in TypeScript support for StyleSheet. We can also use TypeScript types when creating reusable style objects and components.

1. What is StyleSheet?

StyleSheet is a React Native API used to create styles for components.

import { StyleSheet } from "react-native";

const styles = StyleSheet.create({
    container: {
        flex: 1
    }
});

The created styles can be used with components such as View, Text, TextInput, and Image.

2. Importing StyleSheet

Import StyleSheet from React Native.

import { StyleSheet } from "react-native";

TypeScript can understand the React Native StyleSheet API through its available type definitions.

3. Creating a StyleSheet

const styles = StyleSheet.create({
    container: {
        padding: 20
    }
});

The create() method is used to define styles.

4. Using a StyleSheet with View

import {
    View,
    StyleSheet
} from "react-native";

const styles = StyleSheet.create({
    container: {
        padding: 20
    }
});

<View style={styles.container}>
    ...
</View>

5. Using StyleSheet with Text

const styles = StyleSheet.create({
    title: {
        fontSize: 24,
        fontWeight: "bold"
    }
});

<Text style={styles.title}>
    Student Details
</Text>

6. Why Use StyleSheet?

  • Keeps styles organized.
  • Makes styles reusable.
  • Improves code readability.
  • Makes components easier to maintain.
  • Works naturally with React Native components.

7. Basic Style Properties

React Native provides many style properties.

const styles = StyleSheet.create({
    box: {
        width: 200,
        height: 100,
        padding: 10,
        margin: 10
    }
});

TypeScript helps provide editor suggestions while writing these properties.

8. Flexbox Styles

React Native uses Flexbox for layout.

const styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: "row",
        justifyContent: "center",
        alignItems: "center"
    }
});

9. Typed flexDirection

The flexDirection property accepts valid React Native direction values.

const styles = StyleSheet.create({
    row: {
        flexDirection: "row"
    },

    column: {
        flexDirection: "column"
    }
});

Incorrect values can be identified by TypeScript-aware development tools.

10. Text Style Properties

const styles = StyleSheet.create({
    heading: {
        fontSize: 24,
        fontWeight: "bold",
        textAlign: "center"
    }
});

These styles can be applied to a Text component.

11. TextInput Styles

const styles = StyleSheet.create({
    input: {
        borderWidth: 1,
        borderRadius: 8,
        padding: 10,
        marginBottom: 12
    }
});

The style can be reused for multiple TextInput components.

12. Image Styles

const styles = StyleSheet.create({
    image: {
        width: 120,
        height: 120,
        borderRadius: 60
    }
});

StyleSheet can also be used with Image components.

13. Multiple Styles

A StyleSheet can contain multiple named styles.

const styles = StyleSheet.create({
    container: {
        flex: 1,
        padding: 20
    },

    title: {
        fontSize: 24
    },

    input: {
        borderWidth: 1,
        padding: 10
    },

    buttonArea: {
        marginTop: 20
    }
});

14. Applying Multiple Styles

Multiple styles can be combined using an array.

<Text
    style={[
        styles.title,
        styles.center
    ]}
>
    Welcome
</Text>

This is useful when common and specific styles need to be combined.

15. Conditional Styles

Styles can be selected based on application state.

const [active, setActive] =
    useState<boolean>(true);

<View
    style={[
        styles.box,
        active && styles.activeBox
    ]}
>
    ...
</View>

TypeScript ensures that active is a boolean.

16. Creating a Style Type

React Native provides style-related types that can be used when a component accepts custom styles.

import {
    StyleProp,
    ViewStyle
} from "react-native";

These types are useful for reusable components.

17. Using StyleProp<ViewStyle>

A custom component can accept an optional View style.

interface BoxProps {
    style?: StyleProp<ViewStyle>;
}

This allows the parent component to provide additional View styles.

18. Reusable Styled View

import {
    View,
    StyleProp,
    ViewStyle
} from "react-native";

interface BoxProps {
    style?: StyleProp<ViewStyle>;
}

function Box({
    style
}: BoxProps): React.JSX.Element {

    return (
        <View
            style={[
                styles.box,
                style
            ]}
        />
    );
}

19. TextStyle Type

For reusable components that accept Text styles, React Native provides TextStyle.

import {
    StyleProp,
    TextStyle
} from "react-native";

interface TitleProps {
    style?: StyleProp<TextStyle>;
}

20. ImageStyle Type

For reusable Image components, ImageStyle can be used.

import {
    StyleProp,
    ImageStyle
} from "react-native";

interface PhotoProps {
    style?: StyleProp<ImageStyle>;
}

21. Combining Style Types

Different React Native components can have different style types.

interface ComponentProps {
    containerStyle?: StyleProp<ViewStyle>;
    textStyle?: StyleProp<TextStyle>;
}

This allows a reusable component to accept styles for both its container and its text.

22. Style Props with Default Styles

interface CardProps {
    style?: StyleProp<ViewStyle>;
}

function Card({
    style
}: CardProps): React.JSX.Element {

    return (
        <View
            style={[
                styles.card,
                style
            ]}
        >
            ...
        </View>
    );
}

The component keeps its default style while allowing additional styles.

23. Responsive Style Values

Styles can also be calculated from values stored in variables.

const padding: number = 20;

const styles = StyleSheet.create({
    container: {
        padding: padding
    }
});

TypeScript ensures that padding is a number.

24. Common StyleSheet Mistakes

  • Using invalid style property names.
  • Using incorrect value types.
  • Passing TextStyle to a View-only component.
  • Using any instead of proper style types.
  • Overusing inline styles when styles can be reused.
  • Forgetting to import required style types.

25. StyleSheet and TypeScript Checking

TypeScript-aware tools can help identify incorrect style values while writing React Native code.

const styles = StyleSheet.create({
    title: {
        fontSize: 24,
        fontWeight: "bold"
    }
});

Using proper React Native style properties improves code safety and editor assistance.

26. Best Practices for Typed Styles

  • Use StyleSheet.create() for reusable styles.
  • Keep styles organized by component purpose.
  • Use StyleProp<ViewStyle> for custom View style props.
  • Use StyleProp<TextStyle> for custom Text style props.
  • Use StyleProp<ImageStyle> for custom Image style props.
  • Use style arrays when combining styles.
  • Avoid unnecessary use of any.

27. Creating a Reusable Styled Card

import {
    View,
    Text,
    StyleSheet,
    StyleProp,
    ViewStyle
} from "react-native";

interface CardProps {
    title: string;
    style?: StyleProp<ViewStyle>;
}

function Card({
    title,
    style
}: CardProps): React.JSX.Element {

    return (
        <View style={[styles.card, style]}>
            <Text style={styles.title}>
                {title}
            </Text>
        </View>
    );
}

28. Complete Typed StyleSheet Example

import React from "react";

import {
    View,
    Text,
    StyleSheet,
    StyleProp,
    ViewStyle,
    TextStyle
} from "react-native";

interface CardProps {
    title: string;
    description: string;
    style?: StyleProp<ViewStyle>;
    titleStyle?: StyleProp<TextStyle>;
}

function InfoCard({
    title,
    description,
    style,
    titleStyle
}: CardProps): React.JSX.Element {

    return (
        <View style={[styles.card, style]}>

            <Text
                style={[
                    styles.title,
                    titleStyle
                ]}
            >
                {title}
            </Text>

            <Text style={styles.description}>
                {description}
            </Text>

        </View>
    );
}

export default function App(): React.JSX.Element {

    return (
        <View style={styles.container}>

            <InfoCard
                title="React Native"
                description="Learn React Native with TypeScript."
            />

            <InfoCard
                title="TypeScript"
                description="Write safer mobile applications."
                style={styles.secondCard}
                titleStyle={styles.specialTitle}
            />

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20
    },

    card: {
        padding: 20,
        borderWidth: 1,
        borderRadius: 10,
        marginBottom: 15
    },

    secondCard: {
        padding: 25
    },

    title: {
        fontSize: 22,
        fontWeight: "bold",
        marginBottom: 8
    },

    specialTitle: {
        fontSize: 26
    },

    description: {
        fontSize: 16
    }

});

29. Why TypeScript is Useful with StyleSheet

TypeScript helps make styling code easier to understand and maintain. It is especially useful when reusable components accept custom styles.

  • Better style-related editor suggestions
  • Safer custom style props
  • Better reusable components
  • Clearer component APIs
  • Easier refactoring
  • Fewer type-related mistakes

30. Summary

StyleSheet is an important part of React Native development. With TypeScript, we can create reusable and type-safe style props using types such as ViewStyle, TextStyle, and ImageStyle.

You should now understand StyleSheet.create(), reusable styles, style arrays, conditional styles, and typed style props for custom React Native components.

📌 Key Points

  • StyleSheet.create() is used to organize React Native styles.
  • StyleSheet can be used with View, Text, TextInput, Image, and other components.
  • StyleProp<ViewStyle> is useful for custom View style props.
  • StyleProp<TextStyle> is useful for custom Text style props.
  • StyleProp<ImageStyle> is useful for custom Image style props.
  • Style arrays can combine multiple styles.
  • TypeScript makes reusable style props safer.
  • Well-organized styles make React Native applications easier to maintain.

🧠 Quick Quiz

Question: Which React Native API is commonly used to create reusable styles?