|
16 | 16 |
|
17 | 17 | ## Introduction |
18 | 18 |
|
19 | | -BugSplat supports the collection of errors in React applications. The |
| 19 | +BugSplat supports the collection of errors and user feedback in React applications. The |
20 | 20 | @bugsplat/react npm package implements an |
21 | 21 | [ErrorBoundary](https://reactjs.org/docs/error-boundaries.html) |
22 | 22 | component in order to capture rendering errors in child components and |
23 | | -post them to BugSplat where they can be tracked and managed. Adding BugSplat |
24 | | -to your React application is extremely easy. Before getting started please |
25 | | -complete the following tasks: |
| 23 | +post them to BugSplat where they can be tracked and managed. It also provides |
| 24 | +a `useFeedback` hook for collecting user feedback such as titles, descriptions, |
| 25 | +and file attachments. Adding BugSplat to your React application is extremely |
| 26 | +easy. Before getting started please complete the following tasks: |
26 | 27 |
|
27 | 28 | - [Sign up](https://app.bugsplat.com/v2/sign-up) for BugSplat |
28 | 29 | - Create a new |
@@ -253,6 +254,65 @@ function App() { |
253 | 254 | } |
254 | 255 | ``` |
255 | 256 |
|
| 257 | +## User Feedback |
| 258 | + |
| 259 | +Want to collect user feedback such as titles, descriptions, and file |
| 260 | +attachments? Use the `useFeedback` hook to submit feedback directly to your |
| 261 | +BugSplat dashboard. |
| 262 | + |
| 263 | +```jsx |
| 264 | +// src/FeedbackForm.tsx |
| 265 | + |
| 266 | +import { useState } from 'react'; |
| 267 | +import { useFeedback } from '@bugsplat/react'; |
| 268 | + |
| 269 | +export default function FeedbackForm() { |
| 270 | + const [title, setTitle] = useState(''); |
| 271 | + const [description, setDescription] = useState(''); |
| 272 | + const [files, setFiles] = useState<FileList | null>(null); |
| 273 | + const { postFeedback, loading, response, error } = useFeedback(); |
| 274 | + |
| 275 | + const handleSubmit = async (e: React.FormEvent) => { |
| 276 | + e.preventDefault(); |
| 277 | + |
| 278 | + const attachments = files |
| 279 | + ? Array.from(files).map((file) => ({ filename: file.name, data: file })) |
| 280 | + : []; |
| 281 | + |
| 282 | + await postFeedback(title, { description, attachments }); |
| 283 | + }; |
| 284 | + |
| 285 | + if (response) { |
| 286 | + return <p>Thanks for your feedback!</p>; |
| 287 | + } |
| 288 | + |
| 289 | + return ( |
| 290 | + <form onSubmit={handleSubmit}> |
| 291 | + <input |
| 292 | + type="text" |
| 293 | + placeholder="Title" |
| 294 | + value={title} |
| 295 | + onChange={(e) => setTitle(e.target.value)} |
| 296 | + /> |
| 297 | + <textarea |
| 298 | + placeholder="Description" |
| 299 | + value={description} |
| 300 | + onChange={(e) => setDescription(e.target.value)} |
| 301 | + /> |
| 302 | + <input |
| 303 | + type="file" |
| 304 | + multiple |
| 305 | + onChange={(e) => setFiles(e.target.files)} |
| 306 | + /> |
| 307 | + <button type="submit" disabled={loading}> |
| 308 | + {loading ? 'Submitting...' : 'Send Feedback'} |
| 309 | + </button> |
| 310 | + {error && <p>{error.message}</p>} |
| 311 | + </form> |
| 312 | + ); |
| 313 | +} |
| 314 | +``` |
| 315 | + |
256 | 316 | ## Advanced Usage |
257 | 317 |
|
258 | 318 | ### The `scope` property |
@@ -511,6 +571,41 @@ function withErrorBoundary<P extends Record<string, unknown>>( |
511 | 571 | function useErrorHandler(errorProp?: unknown): (error: unknown) => void; |
512 | 572 | ``` |
513 | 573 |
|
| 574 | +### `useFeedback` |
| 575 | +
|
| 576 | +```typescript |
| 577 | +/** |
| 578 | + * Hook for submitting user feedback to BugSplat. |
| 579 | + * |
| 580 | + * @returns Object with `postFeedback` function and `loading`/`response`/`error` state |
| 581 | + * |
| 582 | + * @example |
| 583 | + * const { postFeedback, loading, error } = useFeedback(); |
| 584 | + * await postFeedback('Login button broken', { description: 'Nothing happens when I tap it' }); |
| 585 | + */ |
| 586 | +function useFeedback(): { |
| 587 | + /** |
| 588 | + * Submit user feedback to BugSplat. |
| 589 | + * |
| 590 | + * @param title - Feedback title |
| 591 | + * @param options - Optional BugSplat options (description, attachments, etc.) |
| 592 | + */ |
| 593 | + postFeedback: (title: string, options?: BugSplatOptions) => Promise<void>; |
| 594 | + /** |
| 595 | + * Whether feedback is currently being submitted |
| 596 | + */ |
| 597 | + loading: boolean; |
| 598 | + /** |
| 599 | + * BugSplat response from the most recent feedback submission |
| 600 | + */ |
| 601 | + response: BugSplatResponse | null; |
| 602 | + /** |
| 603 | + * Error from the most recent feedback submission, if any |
| 604 | + */ |
| 605 | + error: Error | null; |
| 606 | +}; |
| 607 | +``` |
| 608 | +
|
514 | 609 | ## Test Suite |
515 | 610 |
|
516 | 611 | This package contains both unit and integration tests. To run them, |
|
0 commit comments