Skip to content

Commit 2a8d5bd

Browse files
authored
Implement React 19 TypeScript example (#1279)
1 parent 40ec52f commit 2a8d5bd

File tree

3 files changed

+107
-21
lines changed

3 files changed

+107
-21
lines changed

examples/react-19-typescript/src/App.tsx

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
4-
1+
import React, { useState } from 'react';
52
import Rollbar from 'rollbar';
6-
const instance: Rollbar = new Rollbar(); // TODO(matux): finish example once import is working
3+
import ErrorBoundary from './ErrorBoundary';
4+
import TestError from './TestError';
75

86
function App() {
7+
const [rollbar] = useState(() => new Rollbar({
8+
accessToken: 'POST_CLIENT_ITEM_TOKEN',
9+
captureUncaught: true,
10+
captureUnhandledRejections: true,
11+
}));
12+
13+
const logInfo = () => {
14+
// Example log event using the rollbar object.
15+
rollbar.info('react test log');
16+
};
17+
18+
const throwError = () => {
19+
// Example error, which will be reported to rollbar.
20+
throw new Error('react test error');
21+
};
22+
923
return (
10-
<div className="App">
11-
<header className="App-header">
12-
<img src={logo} className="App-logo" alt="logo" />
13-
<p>
14-
Edit <code>src/App.tsx</code> and save to reload.
15-
</p>
16-
<a
17-
className="App-link"
18-
href="https://reactjs.org"
19-
target="_blank"
20-
rel="noopener noreferrer"
21-
>
22-
Learn React
23-
</a>
24-
</header>
25-
</div>
24+
<React.Fragment>
25+
<h1>Rollbar Example for React</h1>
26+
<button id="rollbar-info" onClick={logInfo}>
27+
Log Info
28+
</button>
29+
<button id="throw-error" onClick={throwError}>
30+
ThrowError
31+
</button>
32+
<ErrorBoundary rollbar={rollbar}>
33+
<TestError />
34+
</ErrorBoundary>
35+
</React.Fragment>
2636
);
2737
}
2838

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { Component, ReactNode } from 'react';
2+
import Rollbar from 'rollbar';
3+
4+
interface Props {
5+
children: ReactNode;
6+
rollbar: Rollbar;
7+
}
8+
9+
interface State {
10+
hasError: boolean;
11+
}
12+
13+
class ErrorBoundary extends Component<Props, State> {
14+
constructor(props: Props) {
15+
super(props);
16+
this.state = { hasError: false };
17+
}
18+
19+
static getDerivedStateFromError(error: Error): State {
20+
// Update state so the next render will show the fallback UI.
21+
return { hasError: true };
22+
}
23+
24+
componentDidCatch(error: Error, info: React.ErrorInfo) {
25+
this.props.rollbar.info(error.message);
26+
}
27+
28+
render() {
29+
if (this.state.hasError) {
30+
// You can render any custom fallback UI
31+
return <h1>Something went wrong.</h1>;
32+
}
33+
34+
return this.props.children;
35+
}
36+
}
37+
38+
export default ErrorBoundary;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { Component } from 'react';
2+
3+
interface State {
4+
throwError: boolean;
5+
}
6+
7+
class TestError extends Component<{}, State> {
8+
constructor(props: {}) {
9+
super(props);
10+
11+
this.state = { throwError: false };
12+
13+
this.setErrorState = this.setErrorState.bind(this);
14+
}
15+
16+
setErrorState() {
17+
// Use an error state and throw inside render,
18+
// because React won't send errors within event handlers
19+
// to the error boundary component.
20+
this.setState({ throwError: true });
21+
}
22+
23+
render() {
24+
if (this.state.throwError) {
25+
throw new Error('react child test error');
26+
}
27+
return (
28+
<React.Fragment>
29+
<h1>Rollbar Example for React Child Component</h1>
30+
<button id="child-error" onClick={this.setErrorState}>
31+
Throw Child Error
32+
</button>
33+
</React.Fragment>
34+
);
35+
}
36+
}
37+
38+
export default TestError;

0 commit comments

Comments
 (0)