react-livechat icon indicating copy to clipboard operation
react-livechat copied to clipboard

onChatLoaded is only called the first time LiveChat loads

Open meta-meta opened this issue 8 years ago • 1 comments

Here's my use case:

export default class LiveChatApi extends Component {
  componentWillMount() {
    if (window.LC_API) {
      this.liveChat = window.LC_API;
      if (this.liveChat.chat_window_hidden()) {
        this.liveChat.minimize_chat_window();
      }
    }
  }

  componentWillUnmount() {
    if (this.liveChat && !this.liveChat.visitor_engaged()) {
      this.liveChat.hide_chat_window();
    }
  }

  render() {
    return (
      <LiveChat onChatLoaded={ref => this.liveChat = ref} license={12345} />
    );
  }
}

LiveChatApi will be rendered only on certain routes like "contact us". So it is typical for it to mount/unmount multiple times per session. In order to obtain a ref to LC_API on subsequent mounts, I had to look to the window object.

I propose that react-livechat should handle this and invoke onChatLoaded on mount if chat is already loaded.

meta-meta avatar Aug 03 '18 18:08 meta-meta

Docs are updated. No we support onChatLoaded 👍

tomekvenits avatar Jan 03 '19 11:01 tomekvenits