|
1 |
| -# rollbar-react |
2 |
| -React features to enhance using Rollbar.js in React Applications |
| 1 | +# Rollbar React SDK |
| 2 | + |
| 3 | +React features to enhance using Rollbar.js in React Applications. |
| 4 | + |
| 5 | +This SDK provides a wrapper around the base [Rollbar.js] SDK in order to provide an |
| 6 | +SDK that matches the intention of how to build React Apps with a declarative API, features for the latest React API like |
| 7 | +hooks and ErrorBoundaries, as well as simplify using Rollbar within a React SPA. |
| 8 | + |
| 9 | +### In Beta |
| 10 | + |
| 11 | +It is currently in a public Beta release right now as we push towards a 1.0 release that will have all of the features |
| 12 | +we want to provide full capability for using React SDK in your production apps. We expect a 1.0 release to come in the |
| 13 | +next month. |
| 14 | + |
| 15 | +## Setup Instructions |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +These instructions provide an addendum to the [Rollbar.js Setup Instructions]. |
| 20 | + |
| 21 | +After following those 2 steps, you will be ready. |
| 22 | + |
| 23 | +### Install Rollbar React SDK |
| 24 | + |
| 25 | +To install with `npm`: |
| 26 | + |
| 27 | +```shell |
| 28 | +npm install @rollbar/react |
| 29 | +``` |
| 30 | + |
| 31 | +To install with `yarn`: |
| 32 | + |
| 33 | +```shell |
| 34 | +yarn add @rollbar/react |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage and Reference |
| 38 | + |
| 39 | +The React SDK is very new and has not been given the full documentation treatment we expect to get from [Rollbar Docs], |
| 40 | +but that will be coming shortly and a direct link will be put here for your reference. |
| 41 | + |
| 42 | +In the meantime, the basic usage reference is available below. |
| 43 | + |
| 44 | +### `Provider` Component |
| 45 | + |
| 46 | +The `Provider` component is used to wrap your React App so an instance of Rollbar will be made available within your React tree. |
| 47 | + |
| 48 | +This is a common pattern in React using a custom [React Context] that is available to the |
| 49 | +`Components` and `hooks` from this SDK library. |
| 50 | + |
| 51 | +#### Configuration Only Usage |
| 52 | + |
| 53 | +The simplest way to use the `Provider` is to provide a configuration as the `config` prop which will instantiate an |
| 54 | +instance of Rollbar for you and provide that to its child tree: |
| 55 | + |
| 56 | +```javascript |
| 57 | +import React from 'react'; |
| 58 | +import { Provider } from '@rollbar/react'; |
| 59 | + |
| 60 | +// same configuration you would create for the Rollbar.js SDK |
| 61 | +const rollbarConfig = { |
| 62 | + accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN', |
| 63 | + environment: 'production', |
| 64 | +}; |
| 65 | + |
| 66 | +export function App(props) { |
| 67 | + return ( |
| 68 | + <Provider config={rollbarConfig}> |
| 69 | + … |
| 70 | + </Provider> |
| 71 | + ); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +#### Instance Usage |
| 76 | + |
| 77 | +Sometimes you may need to instantiate an instance of Rollbar before adding it to your App tree. In that case use the |
| 78 | +`instance` prop to pass it to the `Provider` like this: |
| 79 | + |
| 80 | +```javascript |
| 81 | +import React from 'react'; |
| 82 | +import Rollbar from 'rollbar'; |
| 83 | +import { Provider } from '@rollbar/react'; |
| 84 | + |
| 85 | +// same configuration you would create for the Rollbar.js SDK |
| 86 | +const rollbarConfig = { |
| 87 | + accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN', |
| 88 | + environment: 'production', |
| 89 | +}; |
| 90 | + |
| 91 | +const rollbar = new Rollbar(rollbarConfig); |
| 92 | + |
| 93 | +export function App(props) { |
| 94 | + return ( |
| 95 | + <Provider instance={rollbar}> |
| 96 | + … |
| 97 | + </Provider> |
| 98 | + ); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +This method will also work with the global Rollbar instance when using the `Rollbar.init(…)` method. |
| 103 | + |
| 104 | +#### React Native Usage |
| 105 | + |
| 106 | +Rollbar provides a [React Native SDK] which also wraps the [Rollbar.js] to provide React Native capabilities based on |
| 107 | +that specific environment. |
| 108 | + |
| 109 | +To use the Rollbar React SDK with the [React Native SDK], pass the instance that it generates to the `Provider`'s |
| 110 | +`instance` prop, like this: |
| 111 | + |
| 112 | +```javascript |
| 113 | +import React from 'react'; |
| 114 | +import { Client } from 'rollbar-react-native'; |
| 115 | +import { Provider } from '@rollbar/react'; |
| 116 | + |
| 117 | +const rollbarClient = new Client('POST_CLIENT_ITEM_ACCESS_TOKEN'); |
| 118 | + |
| 119 | +export function App(props) { |
| 120 | + return ( |
| 121 | + <Provider instance={rollbarClient.rollbar}> |
| 122 | + … |
| 123 | + </Provider> |
| 124 | + ); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### `ErrorBoundary` Component |
| 129 | + |
| 130 | + |
| 131 | +### `RollbarContext` Component |
| 132 | + |
| 133 | + |
| 134 | +### `historyContext` to create `history.listener` |
| 135 | + |
| 136 | + |
| 137 | +### `useRollbar` hook |
| 138 | + |
| 139 | + |
| 140 | +### `useRollbarContext` hook |
| 141 | + |
| 142 | + |
| 143 | +### `useRollbarPerson` hook |
| 144 | + |
| 145 | + |
| 146 | +### `useRollbarCaptureEvent` hook |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +[Rollbar.js]: https://github.com/rollbar/rollbar.js |
| 151 | +[Rollbar.js Setup Instructions]: https://github.com/rollbar/rollbar.js/#setup-instructions |
| 152 | +[Rollbar Docs]: https://docs.rollbar.com |
| 153 | +[React Context]: https://reactjs.org/docs/context.html |
| 154 | +[React Native SDK]: https://github.com/rollbar/rollbar-react-native |
0 commit comments