React
Real Time

useParseQuery

9min

Getting started with the Parse React hook for real time updates using Parse

Introduction

The easiest way to integrate Parse/Back4App into your JavaScript-based project is through the Parse Javascript SDK. This library works on multiple JavaScript environments such as NodeJS, ReactJS, VueJS, AngularJS, React-Native, and gives you access to the Back4App features.

Parse React’s hook goal is to make this experience even better for ReactJS developers by delivering a light-weight and easy-to-use layer that provides minimal configuration and code re-usability.

Using this package will ensure that items like setting up credentials, HTTP requests, real-time synchronization, offline-first interaction are automatically available to your React App. The lib is written entirely in TypeScript, on top of Parse Javascript SDK, and is currently on the Alpha version.

In this initial guide, you will install and set up the @parse/react library on your React project.

@parse/react is currently on the Alpha version. The lib is under testing, so we recommend to proceed with caution. Your feedback is very appreciated, so feel free to use the lib and send us your questions and first impressions by dropping an email to [email protected].

Prerequisites

To complete this tutorial, you will need:

1 - Installation

Install @parse/react and its peer dependency parse in your React application:

# Using yarn yarn add @parse/react parse # Using npm npm install --save @parse/react parse

2 - Application Setup

To allow the App to connect to Back4App servers securely, you must provide Parse JavaScript SDK with App’s credentials in your App.js (or App.tsx) file. Remember to use your Back4App subdomain created when you enabled you app to perform Live Queries.

App.js or App.tsx


Note that the initializeParse method replaces the usual Parse.initialize one. You can find your App Id and JavaScript Key credentials by opening your app Dashboard at Back4App Website and clicking on Core Settings, under Server Settings.

3 - Creating your first Query

Next, you will build your first Query and display it in your App. The @parse/react library exports a useParseQuery hook, so you don’t need to waste time looking into how to implement features like offline support, real-time changes, and so on.

It takes a Parse.Query and returns an object with some props that you can use to access data returned by queries:

App.js or App.tsx


When passing a query to the hook, it will first look for cached results that it might have stored. Then, it creates a WebSocket connection to communicate with the Back4app LiveQuery server, which synchronizes automatically. In other words, the offline-first approach and real-time changes are enabled by default.

To check the query state use the props returned by the hook:

  • isLive: If true, indicates the query have subscribed to realtime updates.
  • isLoading: If true, the query is fetching the results.
  • isSyncing: if true, the query is getting updated results from Back4app servers. results: This is the data returned from the query.
  • count: Indicates the number of objects that matched the query.
  • error: When something goes wrong with the query it returns an error.
  • reload: Reload your query results.

You can see the full documentation for more details on how to set up and use the @parse/react library.

4 - Test the App Hook

Now you should be able to run your React app and see the results.

yarn start

Keep in mind that you should add some data to your Back4App project to see some items in your App.

It’s done!

At this point, you have installed @parse/react on your project, configured the connections with Back4App, and written your first Query. In the next guide, you will see one of the main features of this lib how to use it by creating a sample Live Chat app.