OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.26k stars 170 forks source link

Setting state in React resets to initial state. #154

Closed oluvvafemi closed 2 months ago

oluvvafemi commented 3 months ago

Anytime I set state in React, for example in responseInterceptors, I lose the message and the component rerenders.

const responseInterceptor = (response) => {
  if (response.done) {
    setSomeValue('')
  }
}

For some cases, I set a global variable as a workaround. But is there a solution for when you really need to set a state.

OvidijusParsiunas commented 3 months ago

Hey @oluvvafemi.

This is a standard React practice as changing the state of a component will automatically re-render all of the elements (including Deep Chat) inside it - as well as its message state. The current best solution at the moment is indeed storing the relevant state such as messages inside external state such as an external variable, browser storage, state library or cache. (The DeepChat Playground uses localstorage to retain all data during re-renders and persist it through multiple client sessions, code here)

Looking into the future, I am very inclined to create a solution that would allow the storage of state through configuration so that devs would no longer need to worry about integrating solutions for their state. I have to get through a couple of features before I can get to that one, but I am very committed to exploring it.

If you have any further questions regarding this let me know. Thanks!

oluvvafemi commented 3 months ago

Thank you @OvidijusParsiunas.