Cómo crear una aplicación de tareas en tiempo real con React Native

Una aplicación de TODO toques de todas las partes importantes de la construcción de cualquier aplicación basada en datos, incluyendo el C reate, R ead, U pdate y D operaciones elete (CRUD). En esta historia, crearé una aplicación de tareas pendientes con uno de los marcos móviles más populares, React Native .

Usaré ReactiveSearch Native , una biblioteca de código abierto que proporciona componentes de UI de React Native y simplifica la creación de aplicaciones basadas en datos.

Esto es lo que construiré en esta historia:

Echa un vistazo a la aplicación en bocadillos o en expo.

¿Qué es React Native?

Esto es lo que dicen los doctores:

React Native te permite crear aplicaciones móviles usando solo JavaScript. Utiliza el mismo diseño que React, lo que le permite componer una interfaz de usuario móvil enriquecida a partir de componentes declarativos.

Incluso si recién está comenzando con React o React Native, debería poder seguir esta historia y crear su propia aplicación de tareas pendientes en tiempo real.

¿Por qué utilizar ReactiveSearch? ⚛

ReactiveSearch es una biblioteca de componentes de interfaz de usuario React y React Native de código abierto para Elasticsearch de la que soy coautor con algunas personas increíbles. Proporciona una variedad de componentes React Native que pueden conectarse a cualquier clúster de Elasticsearch.

Escribí otra historia sobre la creación de un explorador de repositorios de GitHub con React y Elasticsearch, que puede consultar para obtener una breve descripción general de Elasticsearch. Incluso si no ha tenido experiencia con Elasticsearch, debería poder seguir esta historia sin problemas.

Configurando las cosas ⚒

Usaremos la versión React Native de la biblioteca aquí.

Antes de comenzar a crear la interfaz de usuario, necesitaremos crear un almacén de datos en Elasticsearch. ReactiveSearch funciona con cualquier índice de Elasticsearch y puede usarlo fácilmente con su propio conjunto de datos.

Para abreviar, puede usar mi conjunto de datos directamente o crear uno para usted usando appbase.io que le permite crear un índice de Elasticsearch alojado (también conocido como aplicación).

Todos los todos están estructurados en el siguiente formato:

{ "title": "react-native", "completed": true, "createdAt": 1518449005768 }

El proyecto de inicio

Antes de comenzar, recomendaría instalar hilo. En Linux, se puede hacer simplemente agregando el repositorio de hilos y ejecutando el comando de instalación a través de su administrador de paquetes. En Mac, primero debe instalar Homebrew para simplificar las cosas. Aquí están los documentos de instalación del hilo para obtener más detalles. Lo siguiente que puede instalar es watchman. Es un servicio de observación de archivos que ayudará al empaquetador react-native a funcionar sin problemas.

He configurado el proyecto de inicio con create-react-native-app en una rama de GitHub aquí. Puede descargar un zip o clonar la rama base ejecutando el siguiente comando:?

git clone -b base //github.com/appbaseio-apps/todos-native
  • A continuación, instale las dependencias e inicie el empaquetador:
cd todos-native && yarn && yarn start
  • Una vez que se inicia el empaquetador, puede ejecutar la aplicación en su teléfono usando la aplicación Expo o usando un emulador de Android o IOS:

¿Buceando en el código?

Una vez que haya clonado el código de la rama base, debería ver una estructura de directorio como la siguiente:

navigation ├── RootComponent.js // Root component for our app ├── MainTabNavigator.js // Tab navigation component screens ├── TodosScreen.js // Renders the TodosContainer components ├── Header.js // Header component ├── AddTodo.js // Add todo input ├── AddTodoButton.js // Add todo floating button ├── TodoItem.js // The todo item ├── TodosContainer.js // Todos main container api ├── todos.js // APIs for performing writes constants // All types of constants used in app types // Todo type to be used with prop-types utils // Streaming logic goes here

Analicemos con qué viene la configuración básica:

1. Navegación

  • Todas las configuraciones necesarias para conectarse a Elasticsearch están en constants/Config.js.
  • Estamos usando TabNavigator de react-navigation para mostrar la pantalla Todos , Activos y Completados . Esto es representado por navigation/RootComponent.js. Notará que RootComponentenvuelve todo dentro del ReactiveBasecomponente de ReactiveSearch. Este componente proporciona todos los datos necesarios a los componentes secundarios de ReactiveSearch. Puede conectar su propio índice de Elasticsearch aquí simplemente actualizando las configuraciones en constants/Config.js.

La lógica de navegación está presente en navigation/MainNavigator.js. Repasemos cómo funciona. Aquí están los documentos para la navegación por pestañas si desea hacer referencia a algo.

import React from 'react'; import { MaterialIcons } from '@expo/vector-icons'; import { TabNavigator, TabBarBottom } from 'react-navigation'; import Colors from '../constants/Colors'; import CONSTANTS from '../constants'; import TodosScreen from '../screens/TodosScreen'; const commonNavigationOptions = ({ navigation }) => ({ header: null, title: navigation.state.routeName, }); // we just pass these to render different routes const routeOptions = { screen: TodosScreen, navigationOptions: commonNavigationOptions, }; // different routes for all, active and completed todos const TabNav = TabNavigator( { [CONSTANTS.ALL]: routeOptions, [CONSTANTS.ACTIVE]: routeOptions, [CONSTANTS.COMPLETED]: routeOptions, }, { navigationOptions: ({ navigation }) => ({ // this tells us which icon to render on the tabs tabBarIcon: ({ focused }) => { const { routeName } = navigation.state; let iconName; switch (routeName) { case CONSTANTS.ALL: iconName = 'format-list-bulleted'; break; case CONSTANTS.ACTIVE: iconName = 'filter-center-focus'; break; case CONSTANTS.COMPLETED: iconName = 'playlist-add-check'; } return (  ); }, }), // for rendering the tabs at bottom tabBarComponent: TabBarBottom, tabBarPosition: 'bottom', animationEnabled: true, swipeEnabled: true, }, ); export default TabNav;
  • La TabNavigatorfunción acepta dos argumentos, el primero son las configuraciones de ruta y el segundo son las TabNavigatorconfiguraciones. En el fragmento anterior, pasamos las configuraciones para mostrar una barra de navegación de pestañas en la parte inferior y establecer diferentes iconos para cada pestaña.

2. TodosScreen y TodosContainer

El TodosScreencomponente screens/TodosScreen.jsenvuelve nuestro TodosContainercomponente principal en el components/TodosContainer.jsque agregaremos varios componentes para la aplicación. El TodosContainermostrará datos filtrados, según si estamos en la pestaña Todo , Activo o Completado .

3. APIs for Creating, Updating and Deleting todos

The APIs for CUD operations on Elasticsearch are present in api/todos.js . It contains three simple methods add, update and destroy which work with any Elasticsearch index as specified in constants/Config.js. An important point to keep in mind is that each todo item we create will have a unique _id field. We can use this _id field for updating or deleting an existing todo.

For our app, we’ll just need three methods for adding, creating or deleting todos. However, you can find a detailed explanation about the API methods at the docs.

Building the components and UI ?

Lets start adding some components to complete the functionality of the app.

1. Adding Todos

We’ll use Fab from native-base to render a floating button for adding todos.

const AddTodoButton = ({ onPress }) => (    );

Now you can use this component in components/TodosContainer.js.

import AddTodoButton from './AddTodoButton'; ... export default class TodosContainer extends React.Component { render() { return (  ...   ); } }

Once we’ve added the button, we’ll see something like this:

Now, when someones clicks on this button, we’ll need to show the input for adding a todo. Lets add the code for this in components/AddTodo.js.

class AddTodo extends Component { constructor(props) { super(props); const { title, completed, createdAt } = this.props.todo; this.state = { title, completed, createdAt, }; } onSubmit = () => { if (this.state.title.length > 0) this.props.onAdd(this.state); return null; }; setStateUtil = (property, value = undefined) => { this.setState({ [property]: value, }); }; render() { const { title, completed } = this.state; const { onBlur } = this.props; return (   this.setStateUtil('completed', !completed)} />   this.setStateUtil('title', changedTitle)} value={title} autoCorrect={false} autoCapitalize="none" onBlur={onBlur} />   this.props.onCancelDelete} style={{ paddingLeft: 25, paddingRight: 15 }} >  0 ? 'black' : 'grey'}`} size={23} />   ); } }

The main components used here are TextInput, Checkbox and Ionicons with straightforward props. We’re using title and completed from the state. We’ll be passing the props todo, onAdd, onCancelDelete and onBlur from the components/TodosContainer.js. These will help us in adding new todos or resetting the view if you wish to cancel adding todos.

Now we can update components/TodosContainer.js with the required changes for rendering AddTodo component:

... import AddTodoButton from './AddTodoButton'; import AddTodo from './AddTodo'; import TodoModel from '../api/todos'; ... // will render todos based on the active screen: all, active or completed export default class TodosContainer extends React.Component { state = { addingTodo: false, }; componentDidMount() { // includes the methods for creation, updation and deletion this.api = new TodoModel('react-todos'); } render() { return (     {this.state.addingTodo ? (   { this.setState({ addingTodo: false }); this.api.add(todo); }} onCancelDelete={() => this.setState({ addingTodo: false })} onBlur={() => this.setState({ addingTodo: false })} />  ) : null}   this.setState({ addingTodo: true })} />  ); } }

The AddTodo component is rendered inside a ScrollView component. We also pass an onPress prop to the AddTodoButton to toggle the state and conditionally display the AddTodo component based on this.state.addingTodo. The onAdd prop passed to AddTodo also creates a new todo using the add API at api/todos.js.

After clicking the add button, we’ll see the input for adding a todo like this:

2. Displaying Todos

After you finish adding a todo, it’s added into Elasticsearch (which we configured in constants/Config.js). All this data can be viewed in realtime by using ReactiveSearch Native components.

There are over 10 native UI components that the library provides. For our todo app, we will primarily utilize the ReactiveList component to show the state of todos.

Lets add the ReactiveList component and get our todos displaying. We’ll add this component in components/TodosContainer.js and the necessary methods for it to work. Here’s how the ReactiveList will be used:

 ... import { ReactiveList } from '@appbaseio/reactivesearch-native'; ... export default class TodosContainer extends React.Component { render() { return (      ({ query: { match_all: {}, }, })} stream onAllData={this.onAllData} dataField="title" showResultStats={false} pagination={false} /> ...   this.setState({ addingTodo: true })} />  ); } }

We haven’t added the onAllData method yet, but let’s understand a bit about the props that we have used here:

  • componentId — unique identifier for the component.
  • defaultQuery: the query to be applied initially for the list. We’ll use match_all to show all the todos in default case.
  • stream: whether to stream new result updates or just show historical results. By setting this to true, we now also listen for the live Todo updates. We’ll add the streaming related logic later.
  • onAllData — a callback function which receives the list of current todo items and the streaming (new todos and any updates) and returns a React component or JSX to render. Here’s how the syntax looks like:

You can read more about all of these props in detail on the ReactiveList’s docs page.

To see something, we’ll need to return a JSX or React component from onAllData callback. For this, we will use React Native’s FlatList which is composed of Text components. In the next step we’ll add our custom TodoItem component.

... import { ScrollView, StyleSheet, StatusBar, FlatList, Text } from 'react-native'; import CONSTANTS from '../constants'; ... export default class TodosContainer extends React.Component { ... onAllData = (todos, streamData) =>  Completed] const filteredData = this.filterTodosData(todos); return (  item._id renderItem={({ item: todo }) => ( {todo.title} )} /> ); }; filterTodosData = (todosData) => { const { screen } = this.props; switch (screen) { case CONSTANTS.ALL: return todosData; case CONSTANTS.ACTIVE: return todosData.filter(todo => !todo.completed); case CONSTANTS.COMPLETED: return todosData.filter(todo => todo.completed); } return todosData; }; render() { ... } }

3. Adding TodoItem(s)

Next, we’ll create a separate component TodoItem for showing each todo which will contain all necessary markups for a todo item like the CheckBox, Text, and a delete Icon. This goes in components/TodoItem.js:

class TodoItem extends Component { onTodoItemToggle = (todo, propAction) => { propAction({ ...todo, completed: !todo.completed, }); }; render() { const { todo, onUpdate, onDelete } = this.props; return (    this.onTodoItemToggle(todo, onUpdate)} style={{ flex: 1, width: '100%', flexDirection: 'row', }} >  this.onTodoItemToggle(todo, onUpdate)} />   {todo.title}     onDelete(todo)} style={{ paddingLeft: 25, paddingRight: 15 }} >  0 ? 'black' : 'grey'}`} size={23} />    ); } }

This component gets the todo from its props along with onDelete and onUpdate which are used to update and delete the todo item respectively. We’re using these at the necessary places using the onPress prop of the components we’re using.

Next, we can import and use the TodoItem component in our onAllData in components/TodosContainer.js. We’ll pass the todo as a prop along with the API methods for update and destroy which will be used by TodoItem component.

class TodosContainer extends Component { ... onAllData = (todos, streamData) => { ... return (  (  )} /> ); } }

4. Streaming Data Updates

You might have noticed that the todos are displaying fine, except you’re unable to view updated todos without refreshing the app. In this final step, we’re going to fit that missing part of the puzzle.

In the previous section, we added an onAllData method for the ReactiveList component. The second parameter of onAllData receives streaming updates which we’re going to utilize to always keep the todos updated. Here’s how the updated onAllData method will look like in components/TodosContainer.js.

import Utils from '../utils'; ... export default class TodosContainer extends React.Component { ... onAllData = (todos, streamData) =>  // merge streaming todos data along with current todos const todosData = Utils.mergeTodos(todos, streamData); // filter data based on "screen": [All  renderItem={({ item: todo }) => (  )} /> ); }; ... }

The mergeTodos method is present in utils/index.js. Here’s how it works:

class Utils { static mergeTodos(todos, streamData) { // generate an array of ids of streamData const streamDataIds = streamData.map(todo => todo._id); return ( todos // consider streamData as the source of truth // first take existing todos which are not present in stream data .filter(({ _id }) => !streamDataIds.includes(_id)) // then add todos from stream data .concat(streamData) // remove todos which are deleted in stream data .filter(todo => !todo._deleted) // finally sort on the basis of creation timestamp .sort((a, b) => a.createdAt - b.createdAt) ); } } export default Utils;

The streamData receives an array of todo objects when they’re created, deleted, or updated. If an object is updated, it contains a _updated key set to true. Similarly, if an object is deleted, it contains a _deleted key set to true. If an object is created, it contains neither of the two. Using these points, we’ve added the mergeTodos function.

With this, you should be able to see the changes to todo items in realtime! If you have an additional device/emulator running the same app, both will stream new updates too. ?

Useful links

  1. Todos app demo, expo link, starter project and final source code
  2. ReactiveSearch GitHub repo ⭐️
  3. ReactiveSearch docs

Hope you enjoyed this story. If you have any thoughts or suggestions, please let me know and have fun!

You may follow me on twitter for latest updates. I've also started posting more recent posts on my personal blog.

Special thanks to Dhruvdutt Jadhav for helping me with this story and the Todos app.