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:
- An app created on Back4App;
- Follow the Enable Live Query tutorial.
- Npm or yarn installed;
- Npx package runner installed.
Step 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
Step 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
1
2
3
4
5
6
7
import { initializeParse } from '@parse/react';
initializeParse(
'YOUR_BACK4APP_SUBDOMAIN', // e.g. YOUR_APP_NAME.b4a.io
'YOUR_APPLICATION_ID',
'YOUR_JAVASCRIPT_KEY'
);
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
.
Step 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
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react';
import Parse from 'parse';
import { initializeParse, useParseQuery } from '@parse/react';
initializeParse(
'YOUR_BACK4APP_SUBDOMAIN', // e.g. YOUR_APP_NAME.b4a.io
'YOUR_APPLICATION_ID',
'YOUR_JAVASCRIPT_KEY'
);
export default function App() {
//make sure your class is enabled for Real Time Notifications (Live Query) checking the menu -> App Settings -> Server Settings -> Server URL and Live Query
const parseQuery = new Parse.Query('YOUR_CLASS_NAME_HERE');
const {
isLive,
isLoading,
isSyncing,
results,
count,
error,
reload
} = useParseQuery(parseQuery);
return (
<div>
{isLoading && (
<p>Loading...</p>
)}
{isLive && (
<p>Live!</p>
)}
{isSyncing && (
<p>Syncing...</p>
)}
{results && (
<ul>
{results.map(result => (
<li key={result.id}>
{result.get('CLASS_COLUMN_NAME_HERE')}
</li>
))}
</ul>
)}
<p>{count}</p>
{error && (
<p>{error.message}</p>
)}
<button
onClick={reload}
>
Reload
</button>
</div>
);
}
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
: Iftrue
, indicates the query have subscribed to realtime updates.isLoading
: Iftrue
, the query is fetching the results.isSyncing
: iftrue
, 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.
Step 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.