1

How can I move the hello world sentence to the middle of the screen

Here are my codes:

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

export default function App(){
  return(
    <View style = {StyleSheet.container}>
      <Text> Hello World</Text>
    </View>
  );
}

{/* stylings of react native*/}

const styles = StyleSheet.create({
  container:{
    flex:1,
    backgroundColor:"blue", 
    alignContent:"center",
    justifyContent:"center"
  }
})

result: Image

3 Answers3

1
return(
    <View style = {styles.container}>  // <-- CHANGE THIS
      <Text> Hello World</Text>
    </View>
  );
D10S
  • 1,468
  • 2
  • 7
  • 13
0

If you also want the text to be in the center horizontally you have to use alignItems:"center" instead of alignContent:"center". Look at this post in case you want more information about the diffence between the two.

PiaBa
  • 155
  • 5
0

Solution

You are doing 2 things wrong

  1. StyleSheet is an API to create styles. You cannot use it to access styles defined inside create method. Instead you need to use styles variable instead.
<View style={StyleSheet.container}> // ❌
<View style={styles.container}> // ✅
  1. You need to use alignItems instead of alignContent to centrally align the Text inside View.
alignContent:"center", // ❌
alignItems:"center", // ✅
Vinay Sharma
  • 3,291
  • 4
  • 30
  • 66