Skip to content

Commit d5380dd

Browse files
authored
fix: made some fixes to how WebChatContainer handles mounting and changes to the web chat config (#15)
- The container will now destroy and recreate web chat when the config changes. - The container will properly destroy and recreate web chat when unmounted and remounted. This address problems caused in development with React 18's strict mode changes.
1 parent 94b4c6b commit d5380dd

11 files changed

+18504
-360
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ module.exports = {
4646
'react/jsx-filename-extension': [1, { extensions: ['.tsx', '.jsx'] }],
4747
'react/require-default-props': 'off',
4848
'no-use-before-define': 'off',
49+
// Airbnb allows arrow functions but not regular functions which doesn't make any sense.
50+
'react/jsx-no-bind': 'off',
51+
// This rule is annoying and sometimes just wrong.
52+
'no-shadow': 'off',
53+
// The properties of object parameters should be allowed to be modified.
54+
'no-param-reassign': ['error', { props: false }],
4955
},
5056
};

README.md

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55

66
`@ibm-watson/assistant-web-chat-react` is a React library to extend the [web chat](https://cloud.ibm.com/docs/watson-assistant?topic=watson-assistant-deploy-web-chat) feature of [IBM Watson Assistant](https://www.ibm.com/cloud/watson-assistant) within your React application. This makes it easier to provide user-defined response types written in React, add content to custom elements with React, have the web chat and your site communicate more easily, and more.
77

8-
There are two utility functions provided by this library:
9-
10-
1. The `WebChatContainer` function is a functional component that makes use of `withWebChat` to load web chat when the component is mounted.
11-
2. The `withWebChat` function creates a high-order-component (HOC) that you can wrap around an existing component to inject `createWebChatInstance` into it so your component can create a new instance of web chat when appropriate. You can find more information in the [withWebChat documentation](./WITH_WEB_CHAT.md).
8+
The primary utility provided by this library is the `WebChatContainer` functional component. This component will load and render an instance of web chat when it is mounted and destroy that instance when unmounted.
129

1310
<details>
1411
<summary>Table of contents</summary>
1512

1613
- [Installation](#installation)
1714
- [Using WebChatContainer](#webchatcontainer)
1815
- [API](#api)
16+
- [withWebChat](#withWebChat)
1917
- [Additional resources](#additional-resources)
2018
- [License](#license)
2119

@@ -40,63 +38,101 @@ yarn add @ibm-watson/assistant-web-chat-react
4038
The `WebChatContainer` function component is intended to make it as easy as possible to include web chat in your React application. To use, you simply need to render this component anywhere in your application and provide the [web chat configuration options object](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-configuration#configurationobject) as a prop.
4139

4240
```javascript
41+
import React from 'react';
42+
import { WebChatContainer, setEnableDebug } from '@ibm-watson/assistant-web-chat-react';
43+
44+
const webChatOptions = {
45+
integrationID: 'XXXX',
46+
region: 'XXXX',
47+
serviceInstanceID: 'XXXX',
48+
// subscriptionID: 'only on enterprise plans',
49+
// Note that there is no onLoad property here. The WebChatContainer component will override it.
50+
// Use the onBeforeRender or onAfterRender prop instead.
51+
};
52+
53+
// Include this if you want to get debugging information.
54+
setEnableDebug(true);
55+
4356
function App() {
44-
const webChatOptions = {
45-
integrationID: 'XXXX',
46-
region: 'XXXX',
47-
serviceInstanceID: 'XXXX',
48-
// subscriptionID: 'only on enterprise plans',
49-
// Note that there is no onLoad property here. The WebChatContainer component will override it with its own.
50-
};
5157
return <WebChatContainer config={webChatOptions} />;
5258
}
5359
```
5460

5561
This component is also capable of managing custom responses. To do so, you need to pass a `renderCustomResponse` function as a prop. This function should return a React component that will render the content for the specific message for that response. You should make sure that the `WebChatContainer` component does not get unmounted in the middle of the life of your application because it will lose all custom responses that were previously received by web chat.
5662

5763
```javascript
64+
import React from 'react';
65+
import { WebChatContainer } from '@ibm-watson/assistant-web-chat-react';
66+
67+
const webChatOptions = {
68+
integrationID: 'XXXX',
69+
region: 'XXXX',
70+
serviceInstanceID: 'XXXX',
71+
// subscriptionID: 'only on enterprise plans',
72+
// Note that there is no onLoad property here. The WebChatContainer component will override it.
73+
// Use the onBeforeRender or onAfterRender prop instead.
74+
};
75+
5876
function App() {
59-
const webChatOptions = {
60-
integrationID: 'XXXX',
61-
region: 'XXXX',
62-
serviceInstanceID: 'XXXX',
63-
// subscriptionID: 'only on enterprise plans',
64-
// Note that there is no onLoad property here. The WebChatContainer component will override it with its own.
65-
};
6677
return <WebChatContainer renderCustomResponse={renderCustomResponse} config={webChatOptions} />;
6778
}
6879

6980
function renderCustomResponse(event) {
70-
return <div>My custom content</div>;
81+
// The event here will contain details for each custom response that needs to be rendered.
82+
// The "user_defined_type" property is just an example; it is not required. You can use any other property or
83+
// condition you want here. This makes it easier to handle different response types if you have more than
84+
// one custom response type.
85+
if (event.data.message.user_defined && event.data.message.user_defined.user_defined_type === 'my-custom-type') {
86+
return <div>My custom content</div>
87+
}
7188
}
7289
```
7390

7491
## API
7592

7693
### WebChatContainer API
7794

78-
The `WebChatContainer` function is a functional component that makes use of `withWebChat` to load web chat when the component is mounted. It can also manage React portals for custom responses.
95+
The `WebChatContainer` function is a functional component that will load and render an instance of web chat when it is mounted and destroy that instance when unmounted. If the web chat configuration options change, it will also destroy the previous web chat and create a new one with the new configuration. It can also manage React portals for custom responses.
96+
97+
Note that this component will call the [web chat render](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-instance-methods#render) method for you. You do not need to call it yourself. You can use the `onBeforeRender` or `onAfterRender` prop to execute operations before or after render is called.
7998

8099
#### Props
81100

82101
`WebChatContainer` has the following props.
83102

84103
| Attribute | Required | Type | Description |
85104
|-----------|----------|---------|-------------|
86-
| config | Yes | object | The [web chat configuration options object](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-configuration#configurationobject). Note that any `onLoad` property will be ignored. |
105+
| config | Yes | object | The [web chat configuration options object](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-configuration#configurationobject). Note that any `onLoad` property will be ignored. If this prop is changed and a new object provided, then the current web chat will be destroyed and a new one created with the new object. |
87106
|
88107
| instanceRef | No | MutableRefObject | A convenience prop that is a reference to the web chat instance. This component will set the value of this ref using the `current` property when the instance has been created. |
89108
|
90-
| onBeforeRender | No | function | This is a callback function that is called after web chat has been loaded and before the `render` function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance functions that are available. |
109+
| onBeforeRender | No | function | This is a callback function that is called after web chat has been loaded and before the `render` function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available. |
110+
|
111+
| onAfterRender | No | function | This is a callback function that is called after web chat has been loaded and after the `render` function is called. This function is passed a single argument which is the instance of web chat that was loaded. This function can be used to obtain a reference to the web chat instance if you want to make use of the instance methods that are available. |
91112
|
92113
| renderCustomResponse | No | function | This function is a callback function that will be called by this container to render custom responses. If this prop is provided, then the container will listen for custom response events from web chat and will generate a React portal for each event. This function will be called once during component render for each custom response event. This function takes two arguments. The first is the [custom response event](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-events#customresponse) that triggered the custom response. The second is a convenience argument that is the instance of web chat. The function should return a `ReactNode` that renders the custom content for the response. |
93114
|
94115

116+
### Debugging
117+
118+
In addition to the props above, the `WebChatContainer` component can output additional debug information. To enable this output, call the `setEnableDebug` function which is exported from this library.
119+
120+
```javascript
121+
setEnableDebug(true);
122+
123+
function App() {
124+
return <WebChatContainer config={webChatOptions} />;
125+
}
126+
```
127+
128+
## withWebChat
129+
130+
The [withWebChat](WITH_WEB_CHAT.MD) function is an older function that is used for loading web chat. It is now deprecated in favor of using the `WebChatContainer` component instead.
131+
95132
## Additional resources
96133
- [Watson Assistant](https://www.ibm.com/cloud/watson-assistant)
97134
- [Watson Assistant web chat feature documentation](https://cloud.ibm.com/docs/watson-assistant?topic=watson-assistant-deploy-web-chat)
98135
- [Watson Assistant web chat API documentation](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-overview)
99-
- [Higher order components](https://reactjs.org/docs/higher-order-components.html)
100136

101137
## License
102138

WITH_WEB_CHAT.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Using withWebChat
22

3+
**NOTE: This function is deprecated and will be removed in a future release of this library. Use the WebChatContainer component instead.**
4+
35
The `WebChatContainer` component, makes use of the `withWebChat` function to create high-order-components that inject a function for creating instances of web chat. This document explains how to use that function if you wish to bypass the `WebChatContainer`.
46

57
<details>

0 commit comments

Comments
 (0)