Skip to content

Commit 4c10a9d

Browse files
authored
docs: made some fixes to the documentation [skip ci] (#16)
[skip ci]
1 parent 2dc38a1 commit 4c10a9d

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

README.md

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ yarn add @ibm-watson/assistant-web-chat-react
3535

3636
## Using WebChatContainer
3737

38+
### Basic example
39+
3840
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.
3941

4042
```javascript
@@ -50,28 +52,62 @@ const webChatOptions = {
5052
// Use the onBeforeRender or onAfterRender prop instead.
5153
};
5254

53-
// Include this if you want to get debugging information.
55+
// Include this if you want to get debugging information from this library. Note this is different than
56+
// the web chat "debug: true" configuration option which enables debugging within web chat.
5457
setEnableDebug(true);
5558

5659
function App() {
5760
return <WebChatContainer config={webChatOptions} />;
5861
}
5962
```
6063

64+
### Accessing instance methods
65+
66+
You can use the `onBeforeRender` or `onAfterRender` props to get access to the instance of web chat if you need call instance methods later. This example renders a button that toggles web chat open and is only rendered after the instance has become available.
67+
68+
```javascript
69+
import React, { useCallback, useState } from 'react';
70+
import { WebChatContainer } from '@ibm-watson/assistant-web-chat-react';
71+
72+
const webChatOptions = { /* Web chat options */ };
73+
74+
function App() {
75+
const [instance, setInstance] = useState(null);
76+
77+
const toggleWebChat = useCallback(() => {
78+
instance.toggleOpen();
79+
}, [instance]);
80+
81+
return (
82+
<>
83+
{instance && (
84+
<button type="button" onClick={toggleWebChat}>
85+
Toggle web chat
86+
</button>
87+
)}
88+
<WebChatContainer config={webChatOptions} onBeforeRender={(instance) => onBeforeRender(instance, setInstance)} />
89+
</>
90+
);
91+
}
92+
93+
function onBeforeRender(instance, setInstance) {
94+
// Make the instance available to the React components.
95+
setInstance(instance);
96+
97+
// Do any other work you might want to do before rendering. If you don't need any other work here, you can just use
98+
// onBeforeRender={setInstance} in the component above.
99+
}
100+
```
101+
102+
### Custom responses
103+
61104
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.
62105

63106
```javascript
64107
import React from 'react';
65108
import { WebChatContainer } from '@ibm-watson/assistant-web-chat-react';
66109

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-
};
110+
const webChatOptions = { /* Web chat options */ };
75111

76112
function App() {
77113
return <WebChatContainer renderCustomResponse={renderCustomResponse} config={webChatOptions} />;
@@ -103,15 +139,10 @@ Note that this component will call the [web chat render](https://web-chat.global
103139
| Attribute | Required | Type | Description |
104140
|-----------|----------|---------|-------------|
105141
| 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. |
106-
|
107142
| 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. |
108-
|
109143
| 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-
|
111144
| 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. |
112-
|
113145
| 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. |
114-
|
115146

116147
### Debugging
117148

0 commit comments

Comments
 (0)