Building a To-Do App with React Native and Firebase

Are you tired of using traditional to-do lists that don’t sync across all your devices? Look no further! In this article, we’ll show you how to build a to-do app using React Native and Firebase, allowing you to access your to-do list from anywhere.

Getting Started with React Native

First, let’s create a new React Native project. Follow the official React Native documentation to set up the basics. Once you’ve completed the setup, create a new React Native project and install the necessary dependencies:


npm install firebase @react-native-community/checkbox

Configuring Firebase

Before we dive into coding, we need to set up Firebase. Create a new file called firebase-config.js in the root directory of your project to implement Firebase configuration and initialization. Then, head to the Firebase website and create a new project. Follow the instructions to set up a Realtime Database and get your Firebase configuration details.

Creating React Components

Now, let’s create our React components. We’ll start with a ToDoItem component to display a task from the to-do list and manage its state. We’ll use the CheckBox component from @react-native-community/checkbox to indicate whether a task has been completed.

“`jsx
import React, { useState } from ‘eact’;
import { View, Text, StyleSheet } from ‘eact-native’;
import { CheckBox } from ‘@react-native-community/checkbox’;

const ToDoItem = () => {
const [doneState, setDone] = useState(false);

const onCheck = () => {
setDone(!doneState);
};

return (



);
};
“`

Next, we’ll create our App component, which will serve as the home screen of our app. We’ll need two pieces of state: todos to store all the to-do items and presentTodo to store the current to-do item being added.

“`jsx
import React, { useState, useEffect } from ‘eact’;
import { View, Text, TextInput, Button, StyleSheet } from ‘eact-native’;
import ToDoItem from ‘./ToDoItem’;

const App = () => {
const [todos, setTodos] = useState({});
const [presentTodo, setPresentTodo] = useState(”);

//…
};
“`

Building a Real-Time Database with Firebase

Now, let’s integrate Firebase into our app. We’ll use the useEffect hook to get all the current to-do items and add them to our todos state object when the component mounts.

“`jsx
import { database } from ‘../firebase-config’;

useEffect(() => {
const todosRef = database.ref(‘todos’);
todosRef.on(‘value’, (snapshot) => {
setTodos(snapshot.val() || {});
});
}, []);
“`

We’ll also add code to our addNewTodo function to add a new to-do item to the database.

jsx
const addNewTodo = () => {
const newTodoRef = database.ref('todos').push();
newTodoRef.set({ name: presentTodo, done: false });
setPresentTodo('');
};

Putting it All Together

Finally, let’s render our ToDoItem components and add some styling to our app.

jsx
return (
<View style={styles.container}>
<TextInput
style={styles.input}
value={presentTodo}
onChangeText={(text) => setPresentTodo(text)}
placeholder="Add new to-do item"
/>
<Button title="Add" onPress={addNewTodo} />
<View style={styles.list}>
{Object.keys(todos).map((key) => (
<ToDoItem key={key} task={todos[key]} />
))}
</View>
</View>
);

That’s it! We’ve created a to-do app with a backend powered by Firebase. You can now access your to-do list from anywhere and sync it across all your devices.

What’s Next?

Want to take your app to the next level? Consider adding authentication to allow users to log in and access their data from any device. You can also explore other features like editing existing to-do items, ordering to-dos, and removing selected to-dos.

Get the Full Source Code

The full source code for this app is available on GitHub. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *