{"id":19403121,"url":"https://github.com/watson-developer-cloud/assistant-web-chat-react","last_synced_at":"2026-04-02T01:55:43.606Z","repository":{"id":39747122,"uuid":"369246559","full_name":"watson-developer-cloud/assistant-web-chat-react","owner":"watson-developer-cloud","description":"A React library to make integration of Watson Assistant web chat with a React application easy.","archived":false,"fork":false,"pushed_at":"2024-07-17T13:48:07.000Z","size":898,"stargazers_count":5,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-15T17:24:26.357Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/watson-developer-cloud.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-20T15:00:49.000Z","updated_at":"2024-08-29T19:33:41.000Z","dependencies_parsed_at":"2024-07-12T15:15:24.440Z","dependency_job_id":"b9a63c83-a6eb-49d0-8579-f91a59c3ee44","html_url":"https://github.com/watson-developer-cloud/assistant-web-chat-react","commit_stats":{"total_commits":15,"total_committers":3,"mean_commits":5.0,"dds":"0.33333333333333337","last_synced_commit":"4c10a9d09c3c08d1c898a038b8ff5ae792624b1a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fassistant-web-chat-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fassistant-web-chat-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fassistant-web-chat-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fassistant-web-chat-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/watson-developer-cloud","download_url":"https://codeload.github.com/watson-developer-cloud/assistant-web-chat-react/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223945851,"owners_count":17229721,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T11:27:18.510Z","updated_at":"2026-04-02T01:55:43.592Z","avatar_url":"https://github.com/watson-developer-cloud.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Build and Test](https://github.com/watson-developer-cloud/assistant-web-chat-react/actions/workflows/build-test.yml/badge.svg)](https://github.com/watson-developer-cloud/assistant-web-chat-react/actions/workflows/build-test.yml)\n\n# IBM watsonx Assistant web chat with React\n\n`@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 watsonx 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.\n\nThe 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.\n\n\u003cdetails\u003e\n  \u003csummary\u003eTable of contents\u003c/summary\u003e\n\n- [Installation](#installation)\n- [Using WebChatContainer](#using-webchatcontainer)\n- [WebChatCustomElement](#webchatcustomelement)\n- [API](#api)\n- [Additional resources](#additional-resources)\n- [License](#license)\n\n\u003c/details\u003e\n\n## Installation\n\nTo install using `npm`:\n\n```bash\nnpm install @ibm-watson/assistant-web-chat-react\n```\n\nOr using `yarn`:\n\n```bash\nyarn add @ibm-watson/assistant-web-chat-react\n```\n\n## Using WebChatContainer\n\n### Basic example\n\nThe `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.\n\n```javascript\nimport React from 'react';\nimport { WebChatContainer, setEnableDebug } from '@ibm-watson/assistant-web-chat-react';\n\nconst webChatOptions = {\n  integrationID: 'XXXX',\n  region: 'XXXX',\n  serviceInstanceID: 'XXXX',\n  // subscriptionID: 'only on enterprise plans',\n  // Note that there is no onLoad property here. The WebChatContainer component will override it.\n  // Use the onBeforeRender or onAfterRender prop instead.\n};\n\n// Include this if you want to get debugging information from this library. Note this is different than\n// the web chat \"debug: true\" configuration option which enables debugging within web chat.\nsetEnableDebug(true);\n\nfunction App() {\n  return \u003cWebChatContainer config={webChatOptions} /\u003e;\n}\n```\n\n### Accessing instance methods\n\nYou 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.\n\n```javascript\nimport React, { useCallback, useState } from 'react';\nimport { WebChatContainer } from '@ibm-watson/assistant-web-chat-react';\n\nconst webChatOptions = { /* Web chat options */ };\n\nfunction App() {\n  const [instance, setInstance] = useState(null);\n\n  const toggleWebChat = useCallback(() =\u003e {\n    instance.toggleOpen();\n  }, [instance]);\n\n  return (\n    \u003c\u003e\n      {instance \u0026\u0026 (\n        \u003cbutton type=\"button\" onClick={toggleWebChat}\u003e\n          Toggle web chat\n        \u003c/button\u003e\n      )}\n      \u003cWebChatContainer config={webChatOptions} onBeforeRender={(instance) =\u003e onBeforeRender(instance, setInstance)} /\u003e\n    \u003c/\u003e\n  );\n}\n\nfunction onBeforeRender(instance, setInstance) {\n  // Make the instance available to the React components.\n  setInstance(instance);\n\n  // Do any other work you might want to do before rendering. If you don't need any other work here, you can just use\n  // onBeforeRender={setInstance} in the component above.\n}\n```\n\n### User defined responses\n\nThis component is also capable of managing user defined responses. To do so, you need to pass a `renderUserDefinedResponse` function as a render 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 user defined responses that were previously received by web chat.\n\nYou should treat the `renderUserDefinedResponse` prop like any typical React render prop; it is different from the `userDefinedResponse` event or a typical event handler. The event is fired only once when web chat initially receives the response from the server. The `renderUserDefinedResponse` prop however is called every time the App re-renders and it should return an up-to-date React component for the provided message item just like the render function would for a typical React component.\n\nNote: in web chat 8.2.0, the custom response event was renamed from `customResponse` to `userDefinedResponse`. If this library detects you are using a prior version of web chat, it will use the `customResponse` event instead of `userDefinedResponse`. \n\n```javascript\nimport React from 'react';\nimport { WebChatContainer } from '@ibm-watson/assistant-web-chat-react';\n\nconst webChatOptions = { /* Web chat options */ };\n\nfunction App() {\n  return \u003cWebChatContainer renderUserDefinedResponse={renderUserDefinedResponse} config={webChatOptions} /\u003e;\n}\n\nfunction renderUserDefinedResponse(event) {\n  // The event here will contain details for each user defined response that needs to be rendered.\n  // The \"user_defined_type\" property is just an example; it is not required. You can use any other property or\n  // condition you want here. This makes it easier to handle different response types if you have more than\n  // one user defined response type.\n  if (event.data.message.user_defined \u0026\u0026 event.data.message.user_defined.user_defined_type === 'my-custom-type') {\n    return \u003cdiv\u003eMy custom content\u003c/div\u003e\n  }\n}\n```\n\n### A note on config objects creating multiple web chat instances\n\nThe `WebChatContainer` component will destroy the current instance of web chat and create a new one if the config object changes. This check is a strict equal check on the config object itself; it does not compare the properties inside of the config object. A common pitfall to fall into is creating the config object inside the component and passing it to `WebChatContainer`. This will cause the container to destroy and recreate the web chat instance every time the component renders because the config object will change.\n\nHere's an example:\n```javascript\nimport React, { useState } from 'react';\nimport { WebChatContainer, setEnableDebug } from '@ibm-watson/assistant-web-chat-react';\n\n// Enable debugging so we can see what WebChatContainer is doing.\nsetEnableDebug(true);\n\nfunction App() {\n  const [value, setValue] = useState(0);\n\n  const config = {\n    integrationID: 'XXX',\n    region: 'XXX',\n    serviceInstanceID: 'XXX',\n  };\n\n  return (\n    \u003cdiv\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e setValue(value + 1)}\u003e\n        Value: {value}\n      \u003c/button\u003e\n      \u003cWebChatContainer config={config} /\u003e\n    \u003c/div\u003e\n  );\n}\n\nexport default App;\n```\n\nIf you click the button in this example, it will trigger a re-render of the component and if you look in the console output you will see the following message (because of `setEnableDebug(true)`):\n```\n[IBM watsonx Assistant WebChatContainer] Creating a new web chat due to configuration change.\n```\n\nThe quick solution to this is to move the config object outside of the component:\n```javascript\nimport React, { useState } from 'react';\nimport { WebChatContainer, setEnableDebug } from '@ibm-watson/assistant-web-chat-react';\n\nsetEnableDebug(true);\n\nconst config = {\n  integrationID: 'XXX',\n  region: 'XXX',\n  serviceInstanceID: 'XXX',\n};\n\nfunction App() {\n  const [value, setValue] = useState(0);\n\n  return (\n    \u003cdiv\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e setValue(value + 1)}\u003e\n        Value: {value}\n      \u003c/button\u003e\n      \u003cWebChatContainer config={config} /\u003e\n    \u003c/div\u003e\n  );\n}\n\nexport default App;\n```\n\nHowever this may not always be feasible because you may have a use case where the config object is not static but contains data that is passed into your component from outside. In that case, you will need to memoize the config object to avoid creating a new one unnecessarily. Also be careful with properties that are functions such as the `onError` property. Those properties need to be memoized as well, usually by using `useCallback`.\n\n```javascript\nimport React, { useState, useMemo } from 'react';\nimport { WebChatContainer, setEnableDebug } from '@ibm-watson/assistant-web-chat-react';\n\nsetEnableDebug(true);\n\nfunction MyComponent({ integrationID, region, serviceInstanceID }) {\n  const [value, setValue] = useState(0);\n\n  // Only create a new config object when the configuration properties change.\n  const config = useMemo(\n    () =\u003e ({\n      integrationID,\n      region,\n      serviceInstanceID,\n    }),\n    [integrationID, region, serviceInstanceID],\n  );\n\n  return (\n    \u003cdiv\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e setValue(value + 1)}\u003e\n        Value: {value}\n      \u003c/button\u003e\n      \u003cWebChatContainer config={config} /\u003e\n    \u003c/div\u003e\n  );\n}\n\nfunction App() {\n  return (\n    \u003cMyComponent\n      integrationID=\"XXX\"\n      region=\"us-south\"\n      serviceInstanceID=\"XXX\"\n    /\u003e\n  );\n}\n\nexport default App;\n```\n\n## WebChatCustomElement\n\nThis library provides the component `WebChatCustomElement` which can be used to aid in rendering web chat inside a custom element. This is needed if you want to be able to change the location where web chat is rendered. This component will render an element in your React app and use that element as the custom element for rendering web chat.\n\nThe default behavior of this component will add and remove a classname from the web chat main window as well as your custom element to control the visibility of web chat when it is opened or closed. It will also inject a `style` tag into your application to define the rules for these classnames. When web chat is closed, a classname will be added to the web chat main window to hide the element and a classname will be added to your custom element to set its width and height to 0 so it doesn't take up space. Note that the custom element should remain visible if you want to use the built-in web chat launcher which is also contained in your custom element. If you don't want these behaviors, then provide your own `onViewChange` prop to `WebChatCustomElement` and provide your own logic for controlling the visibility of web chat. If you want custom animations when web chat is opened and closed, this would be the mechanism to do that.\n\nThe simplest example is this:\n\n```javascript\nimport React from 'react';\nimport { WebChatCustomElement } from '@ibm-watson/assistant-web-chat-react';\n\nimport './App.css';\n\nconst webChatOptions = { /* Web chat options */ };\n\nfunction App() {\n  return \u003cWebChatCustomElement className=\"MyCustomElement\" config={webChatOptions} /\u003e;\n}\n```\n\n```css\n.MyCustomElement {\n  position: absolute;\n  left: 100px;\n  top: 100px;\n  width: 500px;\n  height: 500px;\n}\n```\n\n## API\n\n### WebChatContainer API\n\nThe `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 user defined responses.\n\nNote 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.\n\n#### Props\n\n`WebChatContainer` has the following props.\n\n| Attribute | Required | Type    | Description |\n|-----------|----------|---------|-------------|\n| 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. |\n| 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. |\n| 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. |\n| 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. |\n| renderUserDefinedResponse    | No      | function  | This function is a callback function that will be called by this container to render user defined responses. If this prop is provided, then the container will listen for user defined 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 user defined response event. This function takes two arguments. The first is the [user defined response event](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-events#userDefinedResponse) that triggered the user defined response. The second is a convenience argument that is the instance of web chat. The function should return a `ReactNode` that renders the user defined content for the response. |\n\n`WebChatCustomElement` inherits all of the props from `WebChatContainer`. It also has the following additional optional props.\n\n| Attribute | Type    | Description |\n|-----------|---------|-------------|\n| className    | string  | An optional classname that will be added to the custom element. |\n| id    | string  | An optional id that will be added to the custom element. |\n| onViewChange    | function  | An optional listener for \"view:change\" events. Such a listener is required when using a custom element in order to control the visibility of the web chat main window. If no callback is provided here, a default one will be used that uses some classnames to control web chat and your custom element. You can provide a different callback here if you want custom behavior such as an animation when the main window is opened or closed. Note that this function can only be provided before web chat is loaded. After web chat is loaded, the event handler will not be updated. See the web chat [view:change documentation](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-events#view:change) for more information. Also see the [tutorial for using animiations with custom elements](https://github.com/watson-developer-cloud/assistant-toolkit/tree/master/integrations/webchat/examples/custom-element/client/javascript-animation) for an example of what can be done here. |\n\n### Debugging\n\nIn 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.\n\n```javascript\nsetEnableDebug(true);\n\nfunction App() {\n  return \u003cWebChatContainer config={webChatOptions} /\u003e;\n}\n```\n\n## Additional resources\n- [IBM watsonx Assistant](https://www.ibm.com/cloud/watson-assistant)\n- [IBM watsonx Assistant web chat feature documentation](https://cloud.ibm.com/docs/watson-assistant?topic=watson-assistant-deploy-web-chat)\n- [IBM watsonx Assistant web chat API documentation](https://web-chat.global.assistant.watson.cloud.ibm.com/docs.html?to=api-overview)\n\n## License\n\nThis package is available under the [MIT License](./LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson-developer-cloud%2Fassistant-web-chat-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwatson-developer-cloud%2Fassistant-web-chat-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson-developer-cloud%2Fassistant-web-chat-react/lists"}