Closed K-Jadeja closed 8 months ago
Hi @K-Jadeja. If you want to apply certain properties to Deep Chat as soon as it renders, I would highly recommend using the onComponentRender event. That way you do not have to set an arbitrary timeout and can know exactly when the reference is available.
So your code would look something like this:
let isSet = false;
return (
<div ref={ref}>
<DeepChat
onComponentRender={() => {
if (!isSet) {
const component = ref.current.children[0] as DeepChatCore;
component.initialMessages = [
{role: 'user', text: 'Hi'},
{role: 'ai', text: 'Bye'},
];
isSet = true;
}
}}
request={{url: 'https://test.up.railway.app/test/stream'}}
requestInterceptor={requestInterceptor}
responseInterceptor={responseInterceptor}
requestBodyLimits={{maxMessages: -1}}
stream={true}
/>
</div>
);
The isSet
variable is used to prevent infinite recursion as onComponentRender is triggered every time a property is set.
Let me know if you need any further assistance.
I have thought about this a little more and will update the onComponentRender event to pass the actual component as an argument, so it would be easier to access it. I'll include it in the next release. For now, the above example should work just fine.
Thanks @OvidijusParsiunas!
The code works but I still get these errors Property 'children' does not exist on type 'never'
and 'ref.current' is possibly 'null'.
Did I miss something?
My apologies, for some reason the example NextJs project wasn't giving me that Type error. Please use the following code for better Type inference:
const ref = React.useRef<HTMLDivElement>(null);
let isSet = false;
return (
<div ref={ref}>
<DeepChat
onComponentRender={() => {
if (!isSet && ref.current) {
const component = ref.current.children[0] as DeepChatCore;
component.initialMessages = [
{role: 'user', text: 'Hi'},
{role: 'ai', text: 'Bye'},
];
isSet = true;
}
}}
request={{url: 'https://test.up.railway.app/test/stream'}}
requestInterceptor={requestInterceptor}
responseInterceptor={responseInterceptor}
requestBodyLimits={{maxMessages: -1}}
stream={true}
/>
</div>
);
Thank you so much @OvidijusParsiunas! It works now :)
Happy to hear it helped! I will close this issue. Thanks!
I've been following issue #59 and am trying to wrap the DeepChat element and reference the wrapper element. I am trying to get your first suggestion to work with my code but I am facing a lot of errors like
Property 'children' does not exist on type 'never'
and'ref.current' is possibly 'null'.
Sorry, I am new to typescript and would really appreciate any help getting this to work My code:
My main aim is to use methods like
getMessages()
to store and retrieve chat history from a database.Thanks