1 | import * as React from 'react';
|
2 |
|
3 | const MISSING_CONTEXT_ERROR = "Couldn't find a schedule context.";
|
4 |
|
5 | export const ScheduleUpdateContext = React.createContext<{
|
6 | scheduleUpdate: (callback: () => void) => void;
|
7 | flushUpdates: () => void;
|
8 | }>({
|
9 | scheduleUpdate() {
|
10 | throw new Error(MISSING_CONTEXT_ERROR);
|
11 | },
|
12 | flushUpdates() {
|
13 | throw new Error(MISSING_CONTEXT_ERROR);
|
14 | },
|
15 | });
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export default function useScheduleUpdate(callback: () => void) {
|
25 | const { scheduleUpdate, flushUpdates } = React.useContext(
|
26 | ScheduleUpdateContext
|
27 | );
|
28 |
|
29 | scheduleUpdate(callback);
|
30 |
|
31 | React.useEffect(flushUpdates);
|
32 | }
|