walterholohan / react-native-crisp-chat-sdk

React-Native bridge for Crisp Chat iOS and Android SDK's
MIT License
71 stars 26 forks source link

onClose event / how to reset visible state? #99

Open kylegillen opened 1 year ago

kylegillen commented 1 year ago

This module opens up in its own window / view it seems.

The example folder has a state driven solution to showing and hiding the chat window, but with no onClose event what's the proposed solution to resetting the state?

There's an existing closed issue with the same question and an answer here: https://github.com/walterholohan/react-native-crisp-chat-sdk/issues/17#issuecomment-797669406

But the answer is nonsensical.

kasiri182 commented 1 year ago

You can use show function in Crisp Library for show modal

ElpayetSupervan commented 1 year ago

+1 @nextriot this would be very useful for a centralized top level management of Crisp. At the moment there is also this very annoying useEffect which opens the modal by default when the component is mounted : I wonder why this is necessary 😕 ?

kamilpodlasek commented 1 year ago

I couldn't render the chat for the second time because of this issue, so I ended up counting how many times it was opened and using the show function on every subsequent open. Here's my code if anyone has the same problem:

import { useState, useRef } from 'react'
import { show } from 'react-native-crisp-chat-sdk'

export const useSupportChat = () => {
  const [renderChat, setRenderChat] = useState(false)
  const counter = useRef(0)

  return {
    renderChat,
    showChat: () => {
      // workaround for the weird CrispChat behaviour
      if (counter.current === 0) {
        setRenderChat(true)
      } else {
        show()
      }

      counter.current += 1
    },
  }
}

Then, in the component:

const { renderChat, showChat } = useSupportChat()

{renderChat && <CrispChat />}