Skip to content
简体中文

@mitojs/react

This guide preserves integration examples from earlier SDK releases. Version 4 requires an ES2015-capable runtime and drops Internet Explorer support. Open the v4 quickstart

In the MitoJS React 19 error monitoring demo, trigger a render error, observe the ErrorBoundary fallback and inspect the actual SDK payload captured locally. The demo uses MitoJS 4.0.0 and requires no backend.

If you want to import in weixin miniprograme,please replace @mitojs/browser to @miotjs/wx-mini

Terminal window
# using npm
npm install @mitojs/react @mitojs/browser
# using yarn
yarn add @mitojs/react @mitojs/browser
import React from 'react'
import { init } from '@mitojs/browser'
const MitoInstance = init({
dsn: '/upload',
maxBreadcrumbs: 100,
})

If you're using React 16 or above, you can use ErrorBoundary component to catch render error and automatically send to server.Here are some configurations of ErrorBoundary component that provided by @mitojs/react.

index.tsx

import React from 'react'
import App from './App'
import { MitoProvider } from '@mitojs/react'
import { init } from '@mitojs/browser'
const MitoInstance = init({
dsn: 'https://test.com/yourServer',
maxBreadcrumbs: 100,
})
const APP: React.FC = () => {
return (
<MitoProvider MitoInstance={MitoInstance}>
<App />
</MitoProvider>
)
}

OtherComponent.tsx

ErrorBoundary component will automatically send react error if you set the correct dsn.

import { ErrorBoundary } from '@mitojs/react'
import ChildComponent from './ChildComponent'
export default function OtherComponent() {
const onError = (error: Error, componentStack: string) => {
console.log('triggered is render error')
}
const ErrorFallback = <div>Opps,trigger render error</div>
return (
<>
// this is used index.tsx's MitoInstance
<ErrorBoundary onError={onError} fallback={ErrorFallback}>
<ChildComponent></ChildComponent>
</ErrorBoundary>
</>
)
}

initreturn a BrowserClient, so you can define multiple instances with init.The configuration and hooks between multiple instances does not affect each other.

import React from 'react'
import App from './App'
import { MitoProvider } from '@mitojs/react'
import { init } from '@mitojs/browser'
const MitoInstance_one = init({
dsn: 'https://test.com/yourServer_one',
maxBreadcrumbs: 100,
})
const MitoInstance_two = init({
dsn: 'https://test.com/yourServer_two',
maxBreadcrumbs: 20,
})
const APP: React.FC = () => {
return (
<MitoProvider MitoInstance={MitoInstance_one}>
// this is used MitoInstance_one
<App />
<MitoProvider MitoInstance={MitoInstance_two}>
// this is used MitoInstance_two
<OtherComponent />
</MitoProvider>
</MitoProvider>
)
}

If you want to use in Weixin miniprogram,just replace @mitojs/browser to @mitojs/wx-mini.Just like this:

install

Terminal window
yarn add @mitojs/react @mitojs/wx-mini
import React from 'react'
import App from './App'
import { MitoProvider } from '@mitojs/react'
import { init } from '@mitojs/wx-mini'
const MitoInstance = init({
dsn: 'https://test.com/yourServer',
maxBreadcrumbs: 100,
})
const APP: React.FC = () => {
return (
<MitoProvider MitoInstance={MitoInstance}>
<App />
</MitoProvider>
)
}

CDN way is not recommended.Because @mitojs/web commonjs file is include jsxRuntime code,so it's size is larger than else package.

index.html

<header>
<script src="https://cdn.jsdelivr.net/npm/@mitojs/web/dist/web.min.js"></script>
<script>
MITO.init({
dsn: 'https://test.com/yourServer',
maxBreadcrumbs: 100,
});
</script>
</header>

there is MITO varible automatically mounted on the window when you use cdn in script tag.Then you can use in react component

index.tsx

import React from 'react'
import App from './App'
const MitoInstance = MITO.init({
dsn: 'https://test.com/yourServer',
maxBreadcrumbs: 100,
})
const APP: React.FC = () => {
return (
<MITO.MitoProvider MitoInstance={MitoInstance}>
<App />
</MITO.MitoProvider>
)
}