React Native how to use like <br> inside flex direction row

Viewed 185

I'm new to react native, and I facing a problem to positioning my text. I wanted to have something like this:

Hi, Daffa
XII RPL

but this my result:

Hi, Daffa XII RPL

here's my code:

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

export default function Header() {
  return (
    <View style={styles.header}>
      <Image style={styles.img} source={require('../assets/me.png')} />
      <Text style={styles.text}>Hi, Daffa Quraisy</Text>
      <Text style={styles.kelas}>XII RPL</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    height: 200,
    backgroundColor: '#327fe3',
    flexDirection: 'row',
    alignItems: 'center',
  },
  img: {
    height: 50,
    width: 50,
    marginLeft: 20,
    padding: 0,
  },
  text: {
    color: 'white',
    marginLeft: 18,
    fontWeight: 'bold',
  },
  kelas: {
    color: 'white',
    fontSize: 12,
    marginLeft: 18,
    flexWrap: 'nowrap',
  },
});

thanks for reading this, I'm sorry if my question is kinda stupid, and sorry for my bad english.

3 Answers

Working App: Expo snack

enter image description here

Wrap Text components in View component:

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

export default function Header() {
  return (
    <View style={styles.header}>
      <Image style={styles.img} source={require('./assets/snack-icon.png')} />
      <View>
        <Text style={styles.text}>Hi, Daffa Quraisy</Text>
        <Text style={styles.kelas}>XII RPL</Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    height: 200,
    backgroundColor: '#327fe3',
    flexDirection: 'row',
    alignItems: 'center',
  },
  img: {
    height: 50,
    width: 50,
    marginLeft: 20,
    padding: 0,
  },
  text: {
    color: 'white',
    marginLeft: 18,
    fontWeight: 'bold',
  },
  kelas: {
    color: 'white',
    fontSize: 12,
    marginLeft: 18,
    flexWrap: 'nowrap',
  },
});

you can use {\n} this tag to break line

<View style={styles.header}>
  <Image style={styles.img} source={require('../assets/me.png')} />
  <Text style={styles.text}>Hi, Daffa Quraisy {`\n`} <Text style={styles.kelas}>
   XII RPL</Text>
 </Text>
</View>

The <View> element is the default value column but you header class with change row. New <View> element wrapper texts for your fix.

Related