Skip to content

Commit e09e539

Browse files
committed
docs(hooks): finished hooks docs in README
1 parent c0e91e4 commit e09e539

File tree

1 file changed

+216
-8
lines changed

1 file changed

+216
-8
lines changed

README.md

Lines changed: 216 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ next month.
1616
- [Prerequisites](#prerequisites)
1717
- [Install Rollbar SDK](#install-rollbar-sdk)
1818
- [Usage and Reference](#usage-and-reference)
19+
- [Simplest Usage Possible](#simplest-usage-possible)
1920
- [Components](#components)
2021
- [`Provider` Component](#provider-component)
2122
- [`ErrorBoundary` Component](#errorboundary-component)
@@ -39,7 +40,7 @@ After following those 2 steps, you will be ready.
3940
### Install Rollbar React SDK
4041

4142
We are hosting our `@rollbar` scoped packages in Github Packages. In order to use this you will need to follow the
42-
instructions for [Installing a Package from Github Packages](https://docs.github.com/en/packages/guides/configuring-npm-for-use-with-github-packages#installing-a-package)
43+
instructions for [Installing a Package from Github Packages][1]
4344

4445
In the same directory as your `package.json` file, add a `.npmrc` file with the following:
4546

@@ -59,13 +60,62 @@ To install with `yarn`:
5960
yarn add @rollbar/react
6061
```
6162

63+
To install by adding to `package.json` (as suggested by [Github Packages docs][1]), add the following to your project's
64+
`package.json` file:
65+
66+
```json
67+
68+
"dependencies": {
69+
"@rollbar/react": "^0.7.0",
70+
71+
}
72+
73+
```
74+
75+
then run either using `npm` or `yarn` (or other package manager):
76+
77+
```shell
78+
npm install
79+
# OR
80+
yarn install
81+
```
82+
6283
## Usage and Reference
6384

6485
The React SDK is very new and has not been given the full documentation treatment we expect to get from [Rollbar Docs],
6586
but that will be coming shortly and a direct link will be put here for your reference.
6687

6788
In the meantime, the basic usage reference is available below.
6889

90+
### Simplest Usage Possible
91+
92+
To get you started quickly, here is an example that will get you started right away by providing the easiest and simplest
93+
usage possible:
94+
95+
```javascript
96+
import React from 'react';
97+
import { Provider, ErrorBoundary } from '@rollbar/react'; // <-- Provider imports 'rollbar' for us
98+
99+
// same configuration you would create for the Rollbar.js SDK
100+
const rollbarConfig = {
101+
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
102+
environment: 'production',
103+
};
104+
105+
export default function App() {
106+
return (
107+
{/* Provider instantiates Rollbar client instance handling any uncaught errors or unhandled promises in the browser */}
108+
<Provider config={rollbarConfig}>
109+
{/* ErrorBoundary catches all React errors in the tree below and logs them to Rollbar */}
110+
<ErrorBoundary>
111+
// all other app providers and components - Rollbar will just work
112+
113+
</ErrorBoundary>
114+
</Provider>
115+
);
116+
};
117+
```
118+
69119
## Components
70120

71121
The following components are available as named imports from `@rollbar/react`.
@@ -162,12 +212,12 @@ introduced in React 16.
162212
The `ErrorBoundary` is used to wrap any tree or subtree in your React App to catch React Errors and log them to Rollbar
163213
automatically.
164214

165-
The `ErrorBoundary` relies on the [`Provider`](#provider-component) above for the instance of Rollbar, so it will utilize
215+
The `ErrorBoundary` relies on the [`Provider`] above for the instance of Rollbar, so it will utilize
166216
whatever configuration has been provided.
167217

168218
#### Simple Usage
169219

170-
You can add an `ErrorBoundary` component to the top of your tree right after the `Provider` with no additional props
220+
You can add an `ErrorBoundary` component to the top of your tree right after the [`Provider`] with no additional props
171221
and it will just work:
172222

173223
```javascript
@@ -261,7 +311,7 @@ messages to [Rollbar].
261311
This works for your `ErrorBoundary` from above or any other log or message sent to [Rollbar] while the `RollbarContext`
262312
is mounted on the tree.
263313

264-
Like `ErrorBoundary` above, `RollbarContext` relies on a `Provider` for an instance of a [Rollbar.js] client.
314+
Like `ErrorBoundary` above, `RollbarContext` relies on a [`Provider`] for an instance of a [Rollbar.js] client.
265315

266316
#### Basic Usage
267317

@@ -338,10 +388,6 @@ export default function About(props) {
338388
}
339389
```
340390

341-
#### Controlling when the context is set
342-
343-
- `onRender` (optional) is any value that will be treated as a `Boolean` (truthy or falsey)
344-
345391
## Functions
346392

347393
The following functions are available as named imports from `@rollbar/react`.
@@ -425,26 +471,188 @@ const unlisten = history.listen(historyListener);
425471

426472
## Hooks
427473

474+
The following hooks are available as named imports from `@rollbar/react` for use in [Functional Components] making use of the
475+
[React Hooks API] introduced in React 16.8.
476+
477+
### Reliance on `Provider`
478+
479+
All of these hooks below require there to be a [`Provider`] in the React Tree as an ancestor to the component using the hook.
480+
428481
### `useRollbar` hook
429482

483+
To consume the [Rollbar.js] instance directly from the [`Provider`] in your React Tree and make use of the client API within
484+
your [Functional Component], use the `useRollbar` hook which will return the instance from the currently scoped [React Context].
485+
486+
Here is a basic example:
487+
488+
```javascript
489+
import { useRollbar } from '@rollbar/react';
490+
491+
function ContactDetails({ contactId }) {
492+
const rollbar = useRollbar(); // <-- must have parent Provider
493+
const [contact, setContact] = useState();
494+
495+
useEffect(async () => {
496+
try {
497+
const { data } = await getContactFromApi(contactId);
498+
setContact(data.contact);
499+
} catch (error) {
500+
rollbar.error('Error fetching contact', error, { contactId });
501+
}
502+
}, [contactId]);
503+
504+
return (
505+
<div>
506+
507+
</div>
508+
);
509+
}
510+
```
430511

431512
### `useRollbarContext` hook
432513

514+
As an alternative to the [`RollbarContext`] component, you can use the `useRollbarContext` hook in your [Functional Component]
515+
to set the `context` in the [Rollbar.js] client provided by the [`Provider`] above in the React Tree.
516+
517+
Here's an example of using it in several components:
518+
519+
```javascript
520+
// src/pages/HomePage.js
521+
import { useRollbarContext } from '@rollbar/react';
522+
523+
function HomePage(props) {
524+
useRollbarContext('home#index');
525+
526+
return (
527+
<div>
528+
529+
</div>
530+
);
531+
}
532+
533+
// src/pages/UsersPage.js
534+
import { useRollbarContext } from '@rollbar/react';
535+
import UserTable from '../components/users/UserTable';
536+
537+
function UsersPage(props) {
538+
useRollbarContext('users#list');
539+
540+
return (
541+
<UserTable data={props.users} />
542+
);
543+
}
544+
545+
// src/pages/UserDetailsPage.js
546+
import { useRollbarContext } from '@rollbar/react';
547+
import UserDetails from '../components/users/UserDetails';
548+
549+
function UserDetailsPage(props) {
550+
useRollbarContext('users#details');
551+
552+
return (
553+
<UserDetails user={props.user} />
554+
);
555+
}
556+
```
433557

434558
### `useRollbarPerson` hook
435559

560+
It's very usefull in [Rollbar] to log the identity of a person or user using your App for 2 major reasons:
561+
562+
1. It allows you to know exactly who has been affected by an item or error in your App
563+
2. It allows [Rollbar] to tell you the impact a given item or error is having on your users
564+
565+
To make it convenient and easy to set the identity of a person in your React App, the `@rollbar/react` package
566+
has the `userRollbarPerson` hook.
567+
568+
To use it, simply pass an `Object` that has keys and values used to identify an individual user of your App,
569+
and for any future events or messages logged to [Rollbar] will include that person data attached to the log.
570+
571+
Here is a simple example of using it once the current user has been determined:
572+
573+
```javascript
574+
import { useState } from 'react';
575+
import { useRollbarPerson } from '@rollbar/react';
576+
import LoggedInHome from './LoggedInHome';
577+
import LoggedOutHome from './LoggedOutHome';
578+
579+
function Home() {
580+
const [currentUser, setCurrentUser] = useState();
581+
582+
useEffect(async () => {
583+
const user = await Auth.getCurrentUser();
584+
if (user) {
585+
useRollbarPerson(user);
586+
}
587+
setCurrentUser(user);
588+
});
589+
590+
if (currentUser != null) {
591+
return <LoggedInHome />;
592+
}
593+
594+
return <LoggedOutHome />;
595+
}
596+
```
436597

437598
### `useRollbarCaptureEvent` hook
438599

600+
[Rollbar.js] already provides automated [Telemetry] with the default configuration `autoInstrument: true` in the client
601+
which will capture useful telemetry events and data for you.
602+
603+
To provide more breadcrumbs useful for identifying the cause of an item or error, you can add your own capture events
604+
that will be included in the [Telemetry] of an item in [Rollbar] with the `useRollbarCaptureEvent` hook.
605+
606+
The `useRollbarCaptureEvent` hook is designed to capture a new event in your [Functional Component] any time the
607+
`metadata` or `level` you provide to the hook changes. On rerenders, no event is captured if there is not a change
608+
to the references provided to those 2 arguments (utilizing the `dependencies` array arg underneath within the call to
609+
the built-in React `useEffect` hook).
610+
611+
Here is an example of using `useRollbarCaptureEvent` in the render cycle of a [Functional Component] to send a telemetry
612+
event related to the data that will be rendered in the component
613+
614+
```javascript
615+
import { useEffect, useState } from 'react';
616+
import { userRollbar, useRollbarCaptureEvent, LEVEL_INFO } from '@rollbar/react';
617+
618+
function BookDetails({ bookId }) {
619+
const rollbar = useRollbar(); // <-- must have parent Provider
620+
const [book, setBook] = useState();
621+
622+
useEffect(async () => {
623+
try {
624+
const { data } = await getBook(bookId);
625+
setBook(data.book);
626+
} catch (error) {
627+
rollbar.error('Error fetching book', error, { bookId }); // <-- use rollbar to log errors as normal
628+
}
629+
}, [bookId]);
630+
631+
useRollbarCaptureEvent(book, LEVEL_INFO); // <-- only fires when book changes
632+
633+
return (
634+
<div>
635+
636+
</div>
637+
)
638+
}
639+
```
439640

440641

441642
[Rollbar]: https://rollbar.com
442643
[Rollbar Docs]: https://docs.rollbar.com
443644
[Rollbar.js]: https://github.com/rollbar/rollbar.js
444645
[Rollbar.js Setup Instructions]: https://github.com/rollbar/rollbar.js/#setup-instructions
445646
[React Native SDK]: https://github.com/rollbar/rollbar-react-native
647+
[Telemetry]: https://docs.rollbar.com/docs/rollbarjs-telemetry
648+
[`Provider`]: #provider-component
649+
[`ErrorBoundary`]: #errorboundary-component
650+
[`RollbarContext`]: #rollbarcontext-component
651+
[Functional Components]: https://reactjs.org/docs/components-and-props.html#function-and-class-components
446652
[React Context]: https://reactjs.org/docs/context.html
447653
[Error Boundaries]: https://reactjs.org/docs/error-boundaries.html
654+
[React Hooks API]: https://reactjs.org/docs/hooks-intro.html
448655
[history]: https://www.npmjs.com/package/history
449656
[history.location]: https://github.com/ReactTraining/history/blob/master/docs/api-reference.md#location
450657
[history.action]: https://github.com/ReactTraining/history/blob/master/docs/api-reference.md#action
658+
[1]: https://docs.github.com/en/packages/guides/configuring-npm-for-use-with-github-packages#installing-a-package

0 commit comments

Comments
 (0)