React Native For Dummies
React Native For Dummies
Get started
JAVASCRIPT
ANGULARJS
REACT
EMBER
VUE
NEWSLETTER
Tools used.
React Native: This will be used to build our mobile
application (iOS or Android) in JavaScript.
Project setup.
Navigate to a specific directory on your machine and run the following
commands to generate a new React Native project.
react-native init todo
Navigate to the project directory and start your React Native application
by running the following command. Ensure you have your virtual device
or emulator running.
cd todo
react-native run-android
If all things worked properly, you should have a screen like this
displayed on your Android emulator or device.
Create the src directory containing the components and styles subfolder.
The components folder will contain all of our React components while
the styles folder will contain all of our stylings for the components.
mkdir -p src/{components,styles}
Adding new to-do items
Create a todoStyles.js file in the src/styles directory. This will contain all of the
styles for our Todo component that we will create later. Add the code
below to the file.
src/styles/todoStyles.js
import { StyleSheet } from 'react-native';
src/components/todo.js
import React, { Component } from 'react';
import { TextInput, View } from 'react-native';
import styles from '../styles/todoStyles';
App.js
import React from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
import Todo from './src/components/todo';
src/styles/todoItemStyles.js
import { StyleSheet } from 'react-native';
src/components/todoItem.js
import React from 'react';
import { Text, View, TouchableHighlight } from 'react-native';
import styles from '../styles/todoItemStyles';
Updated src/components/Todo.js
import React, { Component } from 'react';
import { TextInput, View, FlatList } from 'react-native';
import TodoItem from '../components/todoItem';
import styles from '../styles/todoStyles';
Our to-do items should now be visible after they are submitted in
the TextInput.
Our app on the Android emulator should now look like this.
Deleting todo items
Run the following command to install react-native-vector-icons and link
it to our application.
npm install --save react-native-vector-icons
npm install
react-native link
We will need to restart our application after this.
react-native run-android
Save all changes and reload the application. Our application should now
look like this. Clicking the remove icon should remove the to-do item.
Marking to-do items as done
Add the method below to the Todo class in src/components/todo.js. This method
will change the value of done key of a to-do Item object with a particular
index to true or false and update our component's state.
...
markAsDone = (index) => {
this.setState(prevState => ({
todos: prevState.todos.map((item, i) => {
if (i === index) {
item.done = !item.done;
return item;
}
return item;
})
}));
}
...
src/components/todoItem.js
import React from 'react';
import { Text, View, TouchableHighlight } from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import styles from '../styles/todoItemStyles';
const TodoItem = ({
item, index, onPressRemove, markAsDone
}) =>
(
<View style={styles.container} >
<TouchableHighlight
onPress={() => markAsDone(index)}
underlayColor="white"
>
{ item.done ?
<Icon name="check-square-o" size={30} color="#5fb660" /> :
<Icon name="square-o" size={30} color="#808080" />
}
</TouchableHighlight>
<Text style={[
styles.text, {
textDecorationLine: item.done ? 'line-through' : 'none',
color: item.done ? '#808080' : 'black'
}]}
>{item.title}
</Text>
<TouchableHighlight
onPress={() => onPressRemove(index)}
>
<Icon name="remove" size={30} color="#d75452" />
</TouchableHighlight>
</View>
);
85
WRITTEN BY
Devin Robichaux
Follow
Student of life.
Frontend Weekly
Follow
It's really hard to keep up with all the front-end development news out there. Let us help you. We
hand-pick interesting articles related to front-end development. You can also subscribe to our
weekly newsletter at http://frontendweekly.co
More From Medium
A Simple Introduction to Generator Functions
Bashiru Ismail Babatunde
Learn more.
Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here,
expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn
more
Make Medium yours.
Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in
your inbox. Explore
If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to
post your thinking on any topic. Write on Medium
About
Help
Legal