Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Reusing Styles | Section
Styling and Layout in React Native Apps

bookReusing Styles

Pyyhkäise näyttääksesi valikon

As your UI grows, you will start repeating the same styles.

Instead of duplicating them, you can reuse styles to keep your code clean and consistent.

Example:

import { View, Text, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.text}>First item</Text>
      <Text style={styles.text}>Second item</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    padding: 20
  },
  text: {
    fontSize: 16,
    marginBottom: 10
  }
});

Here, the same text style is applied to multiple elements.

You can also combine styles:

const styles = StyleSheet.create({
  text: {
    fontSize: 16
  },
  bold: {
    fontWeight: 'bold'
  }
});

Usage:

<Text style={[styles.text, styles.bold]}>Important</Text>

This lets you build flexible and reusable UI patterns.

Reusing styles helps:

  • Keep your code consistent;
  • Reduce duplication;
  • Make changes easier.
question mark

Why should you reuse styles?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 7
some-alt