Skip to content

Commit 5083d53

Browse files
committed
feat: cleanup
1 parent be2b23f commit 5083d53

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

‎example/src/context/NotificationContext.tsx‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,11 @@ const NotificationContext = createContext<NotificationContextValue | undefined>(
1818
undefined
1919
);
2020

21-
export const useNotificationContext = () => {
22-
const ctx = useContext(NotificationContext);
23-
if (!ctx)
24-
throw new Error(
25-
'useNotificationContext must be used within NotificationProvider'
26-
);
27-
return ctx;
28-
};
29-
3021
export const NotificationProvider = ({ children }: { children: ReactNode }) => {
3122
const [permStatus, setPermStatus] =
3223
useState<PermissionStatus>('undetermined');
33-
const [token, setToken] = useState<string | undefined>(undefined);
34-
const [lastReceived, setLastReceived] = useState<string | undefined>(
35-
undefined
36-
);
24+
const [token, setToken] = useState<string>();
25+
const [lastReceived, setLastReceived] = useState<string>();
3726

3827
useEffect(() => {
3928
let active = true;
@@ -67,12 +56,11 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
6756
}, [permStatus]);
6857

6958
useEffect(() => {
70-
if (permStatus !== 'granted' && permStatus !== 'provisional') return;
7159
const sub = Notifications.addOnNotificationReceived((n) => {
72-
setLastReceived(`${n.title ?? '(no title)'} — ${n.body ?? '(no body)'}`);
60+
setLastReceived(`Received: ${n.title ?? ''} — ${n.body ?? ''}`);
7361
});
7462
return () => sub.remove();
75-
}, [permStatus]);
63+
}, []);
7664

7765
useEffect(() => {
7866
if (permStatus !== 'granted' && permStatus !== 'provisional') return;
@@ -108,3 +96,12 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
10896
</NotificationContext.Provider>
10997
);
11098
};
99+
100+
export const useNotification = () => {
101+
const ctx = useContext(NotificationContext);
102+
if (!ctx)
103+
throw new Error(
104+
'useNotificationContext must be used within NotificationProvider'
105+
);
106+
return ctx;
107+
};

‎example/src/screens/HomeScreen.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import {
99
Text,
1010
} from 'react-native';
1111
import { Section } from '../components/section';
12-
import { useNotificationContext } from '../context/NotificationContext';
12+
import { useNotification } from '../context/NotificationContext';
1313

1414
export const HomeScreen = () => {
1515
const navigation = useNavigation();
1616
const { permStatus, token, lastReceived, requestPermissions, unregister } =
17-
useNotificationContext();
17+
useNotification();
1818

1919
const copyToken = () => {
2020
if (!token) return;
@@ -43,7 +43,7 @@ export const HomeScreen = () => {
4343
</Pressable>
4444

4545
<Section label="Last Received">
46-
<Text testID="last-received-text" style={styles.eventText}>
46+
<Text testID="last-event-text" style={styles.eventText}>
4747
{lastReceived ?? 'None'}
4848
</Text>
4949
</Section>
@@ -65,7 +65,6 @@ const styles = StyleSheet.create({
6565
container: {
6666
flexGrow: 1,
6767
alignItems: 'center',
68-
justifyContent: 'center',
6968
padding: 24,
7069
gap: 24,
7170
},

0 commit comments

Comments
 (0)