Skip to content

Commit a511abb

Browse files
authored
Merge d92ea89 into 9831482
2 parents 9831482 + d92ea89 commit a511abb

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
...
4444
<FeedbackForm/>
4545
```
46+
or auto-inject it by calling the `showFeedbackForm`:
47+
```jsx
48+
import { showFeedbackForm } from '@sentry/react-native';
49+
...
50+
<Button
51+
title="Show feedback form"
52+
onPress={() => {
53+
showFeedbackForm(_props.navigation);
54+
}}
55+
/>
56+
```
4657
Check [the documentation](https://docs.sentry.io/platforms/react-native/user-feedback/) for more configuration options.
4758

4859
- Export `Span` type from `@sentry/types` ([#4345](https://github.com/getsentry/sentry-react-native/pull/4345))

packages/core/src/js/feedback/FeedbackForm.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureFeedback, lastEventId } from '@sentry/core';
1+
import { captureFeedback, lastEventId, logger } from '@sentry/core';
22
import type { SendFeedbackParams } from '@sentry/types';
33
import * as React from 'react';
44
import type { KeyboardTypeOptions } from 'react-native';
@@ -8,6 +8,31 @@ import { defaultConfiguration } from './defaults';
88
import defaultStyles from './FeedbackForm.styles';
99
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
1010

11+
let feedbackFormHandler: (() => void) | null = null;
12+
13+
const setFeedbackFormHandler = (handler: () => void): void => {
14+
feedbackFormHandler = handler;
15+
};
16+
17+
const clearFeedbackFormHandler = (): void => {
18+
feedbackFormHandler = null;
19+
};
20+
21+
type Navigation = {
22+
navigate: (screen: string, params?: Record<string, unknown>) => void;
23+
};
24+
25+
export const showFeedbackForm = (navigation: Navigation): void => {
26+
setFeedbackFormHandler(() => {
27+
navigation?.navigate?.('FeedbackForm');
28+
});
29+
if (feedbackFormHandler) {
30+
feedbackFormHandler();
31+
} else {
32+
logger.error('FeedbackForm handler is not set. Please ensure it is initialized.');
33+
}
34+
};
35+
1136
/**
1237
* @beta
1338
* Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback.
@@ -25,6 +50,13 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
2550
};
2651
}
2752

53+
/**
54+
* Clear the handler when the component unmounts
55+
*/
56+
public componentWillUnmount(): void {
57+
clearFeedbackFormHandler();
58+
}
59+
2860
public handleFeedbackSubmit: () => void = () => {
2961
const { name, email, description } = this.state;
3062
const { onFormClose } = { ...defaultConfiguration, ...this.props };

packages/core/src/js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ export type { TimeToDisplayProps } from './tracing';
8585

8686
export { Mask, Unmask } from './replay/CustomMask';
8787

88-
export { FeedbackForm } from './feedback/FeedbackForm';
88+
export { FeedbackForm, showFeedbackForm } from './feedback/FeedbackForm';

samples/react-native/src/Screens/ErrorsScreen.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from 'react-native';
1313

1414
import * as Sentry from '@sentry/react-native';
15+
import { showFeedbackForm } from '@sentry/react-native';
1516

1617
import { setScopeProperties } from '../setScopeProperties';
1718
import { StackNavigationProp } from '@react-navigation/stack';
@@ -226,6 +227,12 @@ const ErrorsScreen = (_props: Props) => {
226227
_props.navigation.navigate('FeedbackForm');
227228
}}
228229
/>
230+
<Button
231+
title="Feedback form (autoinject)"
232+
onPress={() => {
233+
showFeedbackForm(_props.navigation);
234+
}}
235+
/>
229236
<Button
230237
title="Send user feedback"
231238
onPress={() => {

0 commit comments

Comments
 (0)