Getting started—React tutorial

Updating presence

On the previous page we learned to set up presence with an initial value of { cursor: null }—let’s learn how to update presence by taking this and turning it into realtime cursors.

Updating

To get cursor location we can use browser PointerEvents. Switch to Room.tsx and add a function for onPointerMove that gets your cursor location using clientX/clientY and updates your current presence with the value.

Modify the code in /Room.tsx

Next, create a function for onPointerLeave that sets the cursor value to null.

Modify the code in /Room.tsx

And finally, make sure that our div element spans the screen, add the two events.

Modify the code in /Room.tsx

If you hover over a preview window, you’ll now see the coordinates of your cursor, taken from myPresence! On the next page we’ll use this to render the cursors.