This library allows to render and interact with the LiveChat Chat Widget inside a React application
npm install @livechat/widget-react> This library allows to render and interact with the LiveChat Chat Widget inside a React application.

!Github lerna version


Using npm:
``bash`
npm install @livechat/widget-react
or using yarn:
`bash`
yarn add @livechat/widget-react
`ts
import { LiveChatWidget, EventHandlerPayload } from '@livechat/widget-react'
function App() {
function handleNewEvent(event: EventHandlerPayload<'onNewEvent'>) {
console.log('LiveChatWidget.onNewEvent', event)
}
return (
visibility="maximized"
onNewEvent={handleNewEvent}
/>
)
}
`
#### Config data
All properties described below are used for initialization on the first render and later updates of the chat widget with new values on change.
| Prop | Type |
| ---------------------- | -------------------------------------- |
| license | string (required) |
| customerName | string |
| group | string |
| customerEmail | string |
| chatBetweenGroups | boolean |
| sessionVariables | Record
| visibility | 'maximized' \| 'minimized' \| 'hidden' |
| customIdentityProvider | () => CustomerAuth |
CustomerAuth:
| parameters | type | description |
| ------------- | ---------------------- | -------------------------------------------------------------------------------------- |
| getFreshToken | () => Promise
| getToken | () => Promise
| hasToken | () => Promise
| invalidate | () => Promise
#### Event handlers
All event handlers listed below are registered if provided for the first time. They unregister on the component cleanup or the property value change. Descriptions of all events are available after clicking on the associated links.
- onReady
- onAvailabilityChanged
- onVisibilityChanged
- onCustomerStatusChanged
- onNewEvent
- onFormSubmitted
- onRatingSubmitted
- onGreetingDisplayed
- onGreetingHidden
- onRichMessageButtonClicked
This package exports a set of React Hooks that allows consuming reactive data from the chat widget in any place of the application as long as the LiveChatWidget component is rendered in the tree.
#### useWidgetState
Access the current chat widget availability or visibility state if the chat widget is loaded.
`js
import { useWidgetState } from '@livechat/widget-react'
function App() {
const widgetState = useWidgetState()
if (widgetState) {
return (
#### useWidgetIsReady
Check if the chat widget is ready using the boolean flag
isWidgetReady.`js
import { useWidgetIsReady } from '@livechat/widget-react'function App() {
const isWidgetReady = useWidgetIsReady()
return
Chat Widget is {isWidgetReady ? 'loaded' : 'loading...'}
}
`#### useWidgetChatData
Access the
chatId and threadId of the chat if there's one currently available.`js
import { useWidgetChatData } from '@livechat/widget-react'function App() {
const chatData = useWidgetChatData()
if (chatData) {
return (
{chatData.chatId}
{chatData.threadId}
)
}
}
`#### useWidgetGreeting
Access the current greeting
id and uniqueId if one is currently displayed (received and not hidden).`js
import { useWidgetGreeting } from '@livechat/widget-react'function App() {
const greeting = useWidgetGreeting()
if (greeting) {
return (
{greeting.id}
{greeting.uniqueId}
)
}
}
`#### useWidgetCustomerData
Access the
id, isReturning, status, and sessionVariables of the current customer if the chat widget is loaded.`js
import { useWidgetCustomerData } from '@livechat/widget-react'function App() {
const customerData = useWidgetCustomerData()
if (customerData) {
return (
{customerData.id}
{customerData.isReturning}
{customerData.status}
{Object.entries(customerData.sessionVariables).map(([key, value]) => (
- {value}
))}
)
}
}
`#### Custom Identity Provider
In order to make Custom Identity Provider work, you'll have to properly implement and provide a set of following methods:
-
getToken - resolving Chat Widget token. If you want to cache the token, this should return the cached token instead of a fresh request to https://accounts.livechat.com/customer/token endpoint.
- getFreshToken - resolving Chat Widget token. This should always make a call for a fresh token from https://accounts.livechat.com/customer/token endpoint.
- hasToken - resolving boolean. It determines whether a token has been acquired.
- invalidate - resolving nothing. When called, it should remove the current token. There is no need to do anything else as a new token will be requested by getFreshToken afterwards.##### Example usage
`ts
import { LiveChatWidget } from '@livechat/widget-react'const customIdentityProvider = () => {
const baseAPI = 'YOUR_API_URL'
const userId = '30317220-c72d-11ed-2137-0242ac120002'
const getToken = async () => {
const response = await fetch(
${baseAPI}/getToken/${userId}) const token = await response.json()
console.log('getToken', token)
return token
}
const getFreshToken = async () => {
const response = await fetch(
${baseAPI}/getFreshToken/${userId}) const token = await response.json()
console.log('getFreshToken, token')
return token
}
const hasToken = async () => {
const response = await fetch(
${baseAPI}/hasToken/${userId})
const data = await response.json()
return data
} const invalidateToken = async () => {
const response = await fetch(
${baseAPI}/invalidate/${userId})
const data = await response.json()
console.log(data)
} return {
getToken,
getFreshToken,
hasToken,
invalidate: invalidateToken,
}
}
function App() {
return (
license="12345678"
visibility="maximized"
customIdentityProvider={customIdentityProvider}
/>
)
}
``For more information about Custom Identity Provider, check out https://developers.livechat.com/docs/extending-chat-widget/custom-identity-provider
Pull requests are welcome. For major changes, please open an issue first, so we can discuss what you would like to change. Follow a Contributing guide for more details.
The code and documentation in this project are released under the MIT License.