Skip to content

Commit 2e08ebd

Browse files
Merge pull request #28 from openai/remove-callback-ref
Fix React 18 support + expose OpenAIChatKit via ref
2 parents fab654b + 7ca7454 commit 2e08ebd

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

.changeset/breezy-files-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/chatkit-react': patch
3+
---
4+
5+
Removed usage of React 19 only ref callback cleanup function

.changeset/thick-eggs-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/chatkit-react': minor
3+
---
4+
5+
Exposed a ref to the underlying `OpenAIChatKit` DOM element in the return value of `useChatKit`

packages/chatkit-react/src/ChatKit.tsx

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,46 +45,39 @@ export const ChatKit = React.forwardRef<OpenAIChatKit, ChatKitProps>(
4545
return (
4646
<openai-chatkit
4747
ref={(chatKit) => {
48-
ref.current = chatKit;
4948
control.setInstance(chatKit);
49+
5050
if (typeof forwardedRef === 'function') {
5151
forwardedRef(chatKit);
5252
} else if (forwardedRef) {
5353
forwardedRef.current = chatKit;
5454
}
5555

56-
if (ref.current) {
57-
const abortController = new AbortController();
58-
const events = {
59-
'chatkit.error': 'onError',
60-
'chatkit.response.end': 'onResponseEnd',
61-
'chatkit.response.start': 'onResponseStart',
62-
'chatkit.log': 'onLog',
63-
'chatkit.thread.change': 'onThreadChange',
64-
'chatkit.thread.load.start': 'onThreadLoadStart',
65-
'chatkit.thread.load.end': 'onThreadLoadEnd',
66-
} satisfies {
67-
[K in keyof ChatKitEvents]: ToEventHandlerKey<K>;
68-
};
56+
if (!ref.current) {
57+
return;
58+
}
59+
60+
const events: {
61+
[K in keyof ChatKitEvents]: ToEventHandlerKey<K>;
62+
} = {
63+
'chatkit.error': 'onError',
64+
'chatkit.response.end': 'onResponseEnd',
65+
'chatkit.response.start': 'onResponseStart',
66+
'chatkit.log': 'onLog',
67+
'chatkit.thread.change': 'onThreadChange',
68+
'chatkit.thread.load.start': 'onThreadLoadStart',
69+
'chatkit.thread.load.end': 'onThreadLoadEnd',
70+
};
6971

70-
for (const eventName of Object.keys(
71-
events,
72-
) as (keyof ChatKitEvents)[]) {
73-
ref.current.addEventListener(
74-
eventName,
75-
(e) => {
76-
const handler = control.handlers[events[eventName]];
77-
if (typeof handler === 'function') {
78-
handler(e.detail as any);
79-
}
80-
},
81-
{ signal: abortController.signal },
82-
);
83-
}
72+
const eventNames = Object.keys(events) as (keyof ChatKitEvents)[];
8473

85-
return () => {
86-
abortController.abort();
87-
};
74+
for (const event of eventNames) {
75+
ref.current.addEventListener(event, (e) => {
76+
const handler = control.handlers[events[event]];
77+
if (typeof handler === 'function') {
78+
handler(e.detail as any);
79+
}
80+
});
8881
}
8982
}}
9083
{...htmlProps}

packages/chatkit-react/src/useChatKit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type ChatKitControl = {
5050

5151
export type UseChatKitReturn = ChatKitMethods & {
5252
control: ChatKitControl;
53+
ref: React.RefObject<OpenAIChatKit | null>;
5354
};
5455

5556
export function useChatKit(options: UseChatKitOptions): UseChatKitReturn {
@@ -98,5 +99,8 @@ export function useChatKit(options: UseChatKitOptions): UseChatKitReturn {
9899
};
99100
}, [stableOptions, setInstance]);
100101

101-
return React.useMemo(() => ({ ...methods, control }), [methods, control]);
102+
return React.useMemo(
103+
() => ({ ...methods, control, ref }),
104+
[methods, control],
105+
);
102106
}

0 commit comments

Comments
 (0)