1. 1. Part I – Install React-Native
  2. 2. Part II – Create and Run Project
  3. 3. Part III – Create Login Screen

Creating A Login Screen With React Native

This assumes that you have Node and NPM installed on your PC. If not, you can go to the official website and install them. Then, come back to this page.

Part I – Install React-Native

1
npm install -g create-react-native-app

Part II – Create and Run Project

1
2
3
create-react-native-app myfirstreactnativeapp
cd myfirstreactnativeapp
npm start

Part III – Create Login Screen

In the App.js file that is created, we’re going to want to edit a few details.

At the top of the file, add the following line of code to import the visual components we will utilize to create the page.

1
Import { TextInput, StyleSheet, Button } from 'react-native';

Inside of your App class, you want to add a few things. The first would be to add a constructor for the component. This will allow us to define and keep track of the text input being provided by the user.

1
2
3
4
5
6
7
8
constructor(props) {
super(props);
this.state = {
name: "",
email: "",
password: ""
}
}

Then, we want to update our render method to show the text inputs and submit button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
render(){
return (
<View>
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({name: text})}
placeholder="Name"
/>
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({email: text})}
placeholder="E-Mail"
/>
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({password:text})}
secureTextEntry={true}
placeholder="Password"
/>
<Button
onPress={this.submit}
title="Submit"
color="#841584"
/>
</View>
)
}

Using the StyleSheet component, we’ll also want to add some styling to our inputs. We can do so by adding the following lines of code outside of our App class definition. These styles are applied as properties of the TextInput components inside the App class.

1
2
3
4
5
6
const styles = StyleSheet.create({
input: {
width: 250,
margin: 5
}
});

Finally, we need to create a submit method to handle the press event on the submit button. I have left it blank, since how you pass the e-mail and password is entirely up to you (although hopefully it is in the body of an HTTP Request as opposed to url parameters).

1
2
3
submit() {
//Do Something
}