Getting started with the Parse React hook for real time updates using Parse
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.
In this initial guide, you will install and set up the
@parse/react library on your React project.
@parse/reactis 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]
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
@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
App.tsx) file. Remember to use your Back4App subdomain created when you enabled you app to perform Live Queries.
Note that the
initializeParse method replaces the usual
Parse.initialize one. You can find your
App Id and
Dashboard at Back4App Website and clicking on
Core Settings, under
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:
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:
true, indicates the query have subscribed to realtime updates.
true, the query is fetching the results.
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
Step 4 - Test the App Hook
Now you should be able to run your React app and see the results.
Keep in mind that you should add some data to your Back4App project to see some items in your App.
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.