Advertisement
  1. Code
  2. Mobile Development
  3. React Native Templates

Common React Native App Layouts: Login Page

Scroll to top
This post is part of a series called Common React Native App Layouts.
Common React Native App Layouts: News Feed

In this series, you'll learn how to use React Native to create page layouts commonly used in mobile apps. The layouts you'll be creating won't be functional—instead, the main focus of this series is to get your hands dirty in laying out content in your React Native apps.

If you're new to laying out React Native apps or styling in general, check out our tutorial on layouts in React Native:

To follow this series, I challenge you to try recreating each screen by yourself first before reading my step-by-step instructions in the tutorial. You won't really benefit much from this React Native login form tutorial just by reading it! Try first before looking up the answers here. If you succeed in making it look like the original screen, compare your implementation to mine. Then decide for yourself which one is better!

In this first part of the series, you'll create the following login page:

React Login Form Tutorial react native loginReact Login Form Tutorial react native loginReact Login Form Tutorial react native login

Getting Started

In this React Native login screen tutorial, we'll use the Expo CLI. Expo is a set of tools and services built around React Native and native platforms that help you develop, build, deploy, and quickly iterate on iOS, Android, and web apps from the same JavaScript or TypeScript codebase. 

Expo is the easiest and fastest way to build React Native apps. The official Expo get started guide contains detailed instructions on how to download and configure Expo CLI for the major operating systems.

Create a new project:

1
expo init LoginLayout

You'll be prompted to choose the type of project to initialize. Choose Managed workflow and blank. Wait a few seconds for Expo to install the project files and change the directory into the new project.

1
cd LoginLayout

The project structure should look like this. 

React Login Form Tutorial project structureReact Login Form Tutorial project structureReact Login Form Tutorial project structure

Open App.js to start working on your application. The starting code App.js should look like this:

1
import { StatusBar } from 'expo-status-bar';
2
import React from 'react';
3
import { StyleSheet, Text, View } from 'react-native';
4
5
export default function App() {
6
  return (
7
    <View style={styles.container}>
8
      <Text>Open up App.js to start working on your app!</Text>

9
      <StatusBar style="auto" />
10
    </View>

11
  );
12
}
13
14
const styles = StyleSheet.create({
15
  container: {
16
    flex: 1,
17
    backgroundColor: '#fff',
18
    alignItems: 'center',
19
    justifyContent: 'center',
20
  },
21
});

On the second line of code, we import React to use JSX. Then we import the StyleSheetText, and View components from React Native.

Next is the App function, which returns a view component and a text component as a child of that. <View> is an essential component in React Native and can serve many purposes, such as styling different elements, wrapping elements, or nesting elements. The <View>  element is equivalent to <div> in HTML web development. As you can see, the View component has a property, style = {styles.container}which is used to apply styles to the view.

The <Text> component allows us to render text. 

Layouts With Flexbox

Flexbox is a critical technique in React Native. It's designed to provide a consistent layout on different screen sizes. Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions. (You can learn about CSS Flexbox here.) The defaults are different, though, with the flex direction defaulting to column instead of row and the flex parameter only supporting a single number.

Flex defines how items fill over the available space along the main axis. The available space is divided according to each element's flex property.

We define the flexbox layout at the bottom of the file in the styles constant. The container has the following styles:

  • flex: 1: This defines how elements will fill over space. Space is usually divided according to each element's flex property.
  • justifyContent: "center": This aligns children of a container in the center of the container's main axis.
  • alignItems: "center": This aligns children of a container in the center of the container's cross axis.

User Interface and Common Components

A typical React Native will use the following components.

View a container for other components
Text displays text
Image displays images
Stylesheet provides a way of styling elements 
TextInput an input field
Button a clickable button

Adding an Image

Images in React Native are placed in the assets folder and referenced like this:

1
<Image source={require('.assets//image.png')} />

Let's add an image in the assets folder. This will be used as the logo image. Start by removing the text component, and replace it with the logo image. Don't forget to import the Image component at the top.

1
import React from 'react';
2
import { StyleSheet, Text, View, Image } from 'react-native';
3
4
export default class App extends React.Component {
5
  render(){
6
    return (
7
      <View style={styles.container}>
8
        <Image source = {require("./assets/logo.png")}/>

9
      </View>

10
    );
11
  }
12
}
13

Styling in React Native 

Elements in react Native are styled using JavaScript. All the React Native elements accept a prop named style, which accepts an object with style names and values. These style names and values are similar to those used in CSS, except the names are written using camel casing. For example, React Native uses backgroundColor for the CSS property background-color.

Add the following styles to the image.

1
const styles = StyleSheet.create({
2
  container: {
3
    flex: 1,
4
    backgroundColor: '#fff',
5
    alignItems: 'center',
6
    justifyContent: 'center',
7
   },
8
9
   image :{
10
    marginBottom: 40
11
12
  }
13
    
14
});

Here we add a marginBottom: 40 style to make some space between the image and the text inputs.

Next, add text input fields for the email and password. First, add state properties to hold the email and password. State in React Native is used on components that change over time. For example, the information in the TextInput keeps changing as users input their information. The initial state of the email and password will be empty.

Start by importing useState, as shown below. The useState function will allow our functional components to be stateful.

1
import React, { useState } from 'react';

Then initialize the state by adding the following code in the App function.

1
export default function App() {
2
     const [email, setEmail] = useState('');
3
     const [password, setPassword] = useState('');
4
  return (
5
    // rest of the code

6
  );
7
}

We use a View to wrap each text input for easy styling. Add this code below the image.

1
      <View style={styles.inputView}>
2
        <TextInput
3
          style={styles.TextInput}
4
          placeholder="Email."
5
          placeholderTextColor="#003f5c"
6
          onChangeText={(email) => setEmail(email)}
7
        />

8
      </View>

9
10
      <View style={styles.inputView}>
11
        <TextInput
12
          style={styles.TextInput}
13
          placeholder="Password."
14
          placeholderTextColor="#003f5c"
15
          secureTextEntry={true}
16
          onChangeText={(password) => setPassword(password)}
17
        />

18
      </View>

The setState method will update the state object with whatever information the user has entered. secureTextEntry is set to true to hide the text entered in the email text input for security purposes. 

Add the following styles to the inputView and textInput props.

1
 inputView: {
2
    backgroundColor: "#FFC0CB",
3
    borderRadius: 30,
4
    width: "70%",
5
    height: 45,
6
    marginBottom: 20,
7
    alignItems: "center",
8
  },
9
10
  TextInput: {
11
    height: 50,
12
    flex: 1,
13
    padding: 10,
14
    marginLeft: 20,
15
  }

Add the Forgot Password? button below the text input fields. We'll use  a TouchableOpacity button, which changes opacity when pressed.

1
      <TouchableOpacity>
2
        <Text style={styles.forgot_button}>Forgot Password?</Text>

3
      </TouchableOpacity>

Next, add the following styles for the forgot password button.

1
  forgot_button: {
2
    height: 30,
3
    marginBottom: 30,
4
  }

Finally, add the Login button. Add the following code below the forgot password button.

1
      <TouchableOpacity style={styles.loginBtn}>
2
        <Text style={styles.loginText}>LOGIN</Text>

3
      </TouchableOpacity>

Add the styles for the login button.

1
 loginBtn:
2
  {
3
    width:"80%",
4
    borderRadius:25,
5
    height:50,
6
    alignItems:"center",
7
    justifyContent:"center",
8
    marginTop:40,
9
    backgroundColor:"#FF1493",
10
  }

Here we add a border radius style to make the button circular and a marginTop: 40  property to make a space between the two buttons. We also set a custom height and width. 

The Final Login Screen Code

The final React Native code for the login page, App.js, should look like this:

1
import { StatusBar } from "expo-status-bar";
2
import React, { useState } from "react";
3
import {
4
  StyleSheet,
5
  Text,
6
  View,
7
  Image,
8
  TextInput,
9
  Button,
10
  TouchableOpacity,
11
} from "react-native";
12
13
export default function App() {
14
  const [email, setEmail] = useState("");
15
  const [password, setPassword] = useState("");
16
17
  return (
18
    <View style={styles.container}>
19
      <Image style={styles.image} source={require("./assets/log2.png")} />

20
21
      <StatusBar style="auto" />
22
      <View style={styles.inputView}>
23
        <TextInput
24
          style={styles.TextInput}
25
          placeholder="Email."
26
          placeholderTextColor="#003f5c"
27
          onChangeText={(email) => setEmail(email)}
28
        />

29
      </View>

30
31
      <View style={styles.inputView}>
32
        <TextInput
33
          style={styles.TextInput}
34
          placeholder="Password."
35
          placeholderTextColor="#003f5c"
36
          secureTextEntry={true}
37
          onChangeText={(password) => setPassword(password)}
38
        />

39
      </View>

40
41
      <TouchableOpacity>
42
        <Text style={styles.forgot_button}>Forgot Password?</Text>

43
      </TouchableOpacity>

44
45
      <TouchableOpacity style={styles.loginBtn}>
46
        <Text style={styles.loginText}>LOGIN</Text>

47
      </TouchableOpacity>

48
    </View>

49
  );
50
}
51
52
const styles = StyleSheet.create({
53
  container: {
54
    flex: 1,
55
    backgroundColor: "#fff",
56
    alignItems: "center",
57
    justifyContent: "center",
58
  },
59
60
  image: {
61
    marginBottom: 40,
62
  },
63
64
  inputView: {
65
    backgroundColor: "#FFC0CB",
66
    borderRadius: 30,
67
    width: "70%",
68
    height: 45,
69
    marginBottom: 20,
70
71
    alignItems: "center",
72
  },
73
74
  TextInput: {
75
    height: 50,
76
    flex: 1,
77
    padding: 10,
78
    marginLeft: 20,
79
  },
80
81
  forgot_button: {
82
    height: 30,
83
    marginBottom: 30,
84
  },
85
86
  loginBtn: {
87
    width: "80%",
88
    borderRadius: 25,
89
    height: 50,
90
    alignItems: "center",
91
    justifyContent: "center",
92
    marginTop: 40,
93
    backgroundColor: "#FF1493",
94
  },
95
});

And here is the final look of the app:

React Login Form Tutorial react native loginReact Login Form Tutorial react native loginReact Login Form Tutorial react native login

Popular React Native App Layout Templates From CodeCanyon

Building a great app takes more than just a React Native login screen. If you need more components to complete your project, check out these templates from CodeCanyon. They're easy to use and can help you reach your goals.

1. MStore Pro: Complete React Native Template for eCommerce

MStore Pro is one of the most popular React Native templates available on CodeCanyon. It's designed with eCommerce in mind. It has a useful React Native login screen template that works with SMS signup. It also has multiple home screens and layouts to choose from.

MStore Pro React Native Template for eCommerceMStore Pro React Native Template for eCommerceMStore Pro React Native Template for eCommerce

2. Antiqueruby React Native Material Design UI Components

Material design still dominates the app landscape, which makes Antiqueruby an excellent choice in 2021. This download includes over 260 screens, including React Native logins, navigation drawers, and more.

Antiqueruby React Native Material Design UI ComponentsAntiqueruby React Native Material Design UI ComponentsAntiqueruby React Native Material Design UI Components

3. BeoNews Pro: React Native Mobile App for WordPress

If you have a WordPress site that you want to be mobile, try out BeoNews Pro. It's one of the top templates on CodeCanyon, and it's filled with great features. In addition to its React Native login screen templates, BeoNews Pro includes:

  • multiple side menu styles
  • stylish UI layouts
  • Facebook, Google, and Firebase analytics integration
BeoNews Pro React Native Mobile App for WordPressBeoNews Pro React Native Mobile App for WordPressBeoNews Pro React Native Mobile App for WordPress

4. Oreo Fashion: Full React Native App for WooCommerce

Show off your style sense with Oreo Fashion. It's a full template that lets you create apps for iOS and Android devices. The React Native login forms included offer simple social sign-up for account creation. You can also take advantage of the layout app builder. Try it if you have a WooCommerce WordPress site you want to build an app around.

Oreo Fashion Full React Native App for WooCommerceOreo Fashion Full React Native App for WooCommerceOreo Fashion Full React Native App for WooCommerce

Conclusion

In this tutorial, you've successfully created a beautiful login page using your Flexbox knowledge. You've also learned how to use the Expo CLI, an easy way to build and test React Native Apps.

In the next tutorial in this series, you'll learn how to create a calendar screen. In the meantime, check out some of our other tutorials on React Native and Flexbox.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.