Unverified Commit 710997d4 authored by TodorTotev's avatar TodorTotev Committed by GitHub

refactor(with-sentry): example (#13434)

1. I updated all the packages.
2. Refactored the home component from class to functional
3. Example now uses getServerSideProps instead of getInitialProps
parent c01a93ad
...@@ -8,15 +8,15 @@ ...@@ -8,15 +8,15 @@
}, },
"dependencies": { "dependencies": {
"@sentry/browser": "^5.0.3", "@sentry/browser": "^5.0.3",
"@sentry/node": "^5.0.3",
"@sentry/integrations": "^5.0.3", "@sentry/integrations": "^5.0.3",
"@sentry/node": "^5.0.3",
"@zeit/next-source-maps": "0.0.4-canary.1", "@zeit/next-source-maps": "0.0.4-canary.1",
"cookie-parser": "1.4.4", "cookie-parser": "1.4.4",
"express": "^4.16.4", "express": "^4.16.4",
"js-cookie": "2.2.0", "js-cookie": "2.2.0",
"next": "latest", "next": "latest",
"react": "^16.8.0", "react": "16.13.1",
"react-dom": "^16.8.0", "react-dom": "16.13.1",
"sentry-testkit": "^2.1.0", "sentry-testkit": "^2.1.0",
"uuid": "^3.3.2" "uuid": "^3.3.2"
}, },
......
import { Component } from 'react' import { useEffect, useState } from 'react'
import Link from 'next/link' import Link from 'next/link'
class Index extends Component { const Index = () => {
static getInitialProps({ query, req }) { const [state, setState] = useState({
if (query.raiseError) { raiseErrorInUseEffectHook: null,
throw new Error('Error in getInitialProps') })
}
}
state = {
raiseError: false,
}
componentDidUpdate() { useEffect(() => {
if (this.state.raiseErrorInUpdate) { if (state.raiseErrorInUseEffectHook) {
throw new Error('Error in componentDidUpdate') throw new Error('Error in useEffect Hook')
} }
} }, [state.raiseErrorInUseEffectHook])
raiseErrorInUpdate = () => this.setState({ raiseErrorInUpdate: '1' }) return (
raiseErrorInRender = () => this.setState({ raiseErrorInRender: '1' }) <div>
<h2>Index page</h2>
<ul>
<li>
<a
href="#"
onClick={() =>
setState((current) => ({
...current,
raiseErrorInUseEffectHook: '1',
}))
}
>
Raise the error in render or update
</a>
</li>
<li>
<Link href={{ pathname: '/', query: { raiseError: '1' } }}>
<a>Raise error in getServerSideProps</a>
</Link>
</li>
</ul>
</div>
)
}
render() { export async function getServerSideProps({ query }) {
if (this.state.raiseErrorInRender) { if (query.raiseError) {
throw new Error('Error in render') throw new Error('Error in getServerSideProps')
} }
return ( return {
<div> props: {},
<h2>Index page</h2>
<ul>
<li>
<a href="#" onClick={this.raiseErrorInRender}>
Raise the error in render
</a>
</li>
<li>
<a href="#" onClick={this.raiseErrorInUpdate}>
Raise the error in componentDidUpdate
</a>
</li>
<li>
<Link href={{ pathname: '/', query: { raiseError: '1' } }}>
<a>Raise error in getInitialProps of client-loaded page</a>
</Link>
</li>
<li>
<a href="/?raiseError=1">
Raise error in getInitialProps of server-loaded page
</a>
</li>
</ul>
</div>
)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment