Skip to content

Commit

Permalink
Fix backgrounding scenario listening on load
Browse files Browse the repository at this point in the history
The AppState listener was added when the file was imported, which
happens in every scenario

This now uses a useEffect hook so the subscription only happens when the
component is mounted and unsubscribes when unmounted
  • Loading branch information
imjoehaines committed Sep 13, 2023
1 parent d529de5 commit ea762e3
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BugsnagPerformance from '@bugsnag/react-native-performance'
import React from 'react'
import React, { useEffect } from 'react'
import { AppState, Text, View } from 'react-native'

export const config = {
Expand All @@ -8,13 +8,19 @@ export const config = {
appVersion: '1.2.3'
}

AppState.addEventListener('change', (state) => {
if (state === 'background') {
export const App = () => {
useEffect(() => {
const handler = (state) => {
if (state === 'background') {
BugsnagPerformance.startSpan('BackgroundSpan').end()
}
}
})

export const App = () => {
AppState.addEventListener('change', handler)

return () => { AppState.removeEventListener('change', handler) }
}, [])

return (
<View>
<Text>BackgroundSpanScenario</Text>
Expand Down

0 comments on commit ea762e3

Please sign in to comment.