2 |

3 |
@pleak/react-perf-monitor
4 |
Performance monitoring for React and React Native apps with Pleak.
5 |
6 |
7 | # Table of contents
8 |
9 | * [Getting Started](#getting-started)
10 | * [Installation](#installation)
11 | * [React Native](#react-native)
12 | * [Initializing](#initializing)
13 | * [Options](#options)
14 | * [Required](#required)
15 | * [`uri`](#uri)
16 | * [Optional](#optional)
17 | * [`debug`](#debug)
18 | * [`publish`](#publish)
19 | * [`interval`](#interval)
20 | * [`environment`](#environment)
21 | * [Usage](#usage)
22 |
23 | # Getting started
24 |
25 | ## Installation
26 |
27 | ```
28 | # With npm
29 | npm install @pleak/react-perf-monitor
30 |
31 | # With yarn
32 | yarn add @pleak/react-perf-monitor
33 | ```
34 |
35 | ### React Native
36 |
37 | If you're using this package with a React Native app, you must link native dependencies to your project with [react-native-cli](https://www.npmjs.com/package/react-native-cli).
38 |
39 | ```
40 | react-native link
41 | ```
42 |
43 | This command will automatically find native dependencies and link them to your project.
44 |
45 | ## Initializing
46 |
47 | We recommend you to initialize the lib in a separate file and then import it when you need it.
48 |
49 | ```js
50 | import { Pleak } from '@pleak/react-perf-monitor';
51 |
52 | const pleak = new Pleak({
53 | uri: 'YOUR_PLEAK_DSN',
54 | });
55 |
56 | export default pleak;
57 | ```
58 |
59 | ### Options
60 |
61 | #### **Required**
62 |
63 | #### `uri`
64 |
65 | Your Pleak DSN, required to publish to Pleak. The structure of your DSN should look like this:
66 |
67 | ```
68 | https://{publicKey}@{host}/{appId}
69 | ```
70 |
71 | #### **Optional**
72 |
73 | #### `debug`
74 |
75 | _Defaults to false_
76 |
77 | If true, informations about events and publishing will be logged in console.
78 |
79 | #### `publish`
80 |
81 | _Defaults to true_
82 |
83 | If true, collected events will be published on Pleak.
84 |
85 | #### `interval`
86 |
87 | _Defaults to 5000_
88 |
89 | Events are not published one by one, they are stored and published in batch at an interval in milliseconds defined by this option.
90 |
91 | #### `environment`
92 |
93 | _Defaults to `process.env.NODE_ENV`_
94 |
95 | Define tracked environment of your app in Pleak.
96 |
97 | # Usage
98 |
99 | Once you installed and initialized the lib you can use it to monitor your React components like so:
100 |
101 | ```js
102 | import React, { Component } from 'react';
103 | import pleak from '../pleak'; // Import the Pleak instance you defined earlier
104 |
105 | class MyComponent extends Component {
106 | state = { user: null };
107 |
108 | constructor(props) {
109 | super(props);
110 |
111 | // Capture your component's performance
112 | pleak.captureComponentPerfs(this, {
113 | // Optional. Use the excludes option to avoid collecting events on specific methods
114 | excludes: ['render'],
115 | });
116 | }
117 |
118 | componentDidMount() {
119 | /* Optional.
120 | This allows you to attach a context to any event triggered by this method */
121 | pleak.setContext({
122 | time: Date.now(),
123 | });
124 |
125 | this.loadData();
126 | }
127 |
128 | loadData = async () => {
129 | const res = await fetch('https://jsonplaceholder.typicode.com/users/1');
130 | const user = await res.json();
131 |
132 | /* Optional.
133 | This allows you to attach a context to all events,
134 | from the moment when this method is triggered (overwritable) */
135 | pleak.setGlobalContext({
136 | user,
137 | });
138 |
139 | this.setState({
140 | user,
141 | });
142 | };
143 |
144 | render() {
145 | const { user } = this.state;
146 |
147 | return