React Intercom Integration with Hooks

5.5.0 · active · verified Sun Apr 19

react-use-intercom is a specialized React library that provides a hook-driven abstraction layer for integrating the Intercom chat widget into web applications. Currently at version 5.5.0, it maintains an active and agile release cadence, frequently publishing minor and patch updates to support new IntercomJS features, address bugs, and improve compatibility. Key differentiators include its adherence to modern React patterns by leveraging hooks, full TypeScript support for enhanced developer experience, a minimal bundle size with no external dependencies, and built-in safeguards designed to prevent common issues in Server-Side Rendering (SSR) environments like Next.js and Gatsby. It also allows for seamless integration with existing Intercom instances, such as those loaded via Segment. The library aims for a direct, one-to-one mapping of Intercom's vanilla JavaScript API methods to simplify usage while maintaining full access to Intercom's capabilities. Developers should note that many issues might stem from the underlying IntercomJS library itself.

Common errors

Warnings

Install

Imports

Quickstart

This code demonstrates how to initialize Intercom using IntercomProvider, boot the messenger, and control its visibility using the useIntercom hook.

import * as React from 'react';
import { IntercomProvider, useIntercom } from 'react-use-intercom';

const INTERCOM_APP_ID = process.env.INTERCOM_APP_ID ?? 'your-intercom-app-id';

const App = () => (
  <IntercomProvider appId={INTERCOM_APP_ID}>
    <HomePage />
  </IntercomProvider>
);

const HomePage = () => {
  const { boot, shutdown, hide, show, update, showNewMessage } = useIntercom();

  return (
    <div>
      <button onClick={() => boot({ hideDefaultLauncher: false })}>Boot Intercom! ☎️</button>
      <button onClick={hide}>Hide Messenger</button>
      <button onClick={show}>Show Messenger</button>
      <button onClick={() => showNewMessage('Hello there!')}>New Message</button>
      <button onClick={shutdown}>Shutdown Intercom</button>
      <p>This button boots Intercom and shows the messenger. Other buttons control its visibility or initiate new conversations.</p>
    </div>
  );
};

// To run this in a simple environment, you might need a root element and ReactDOM.
// For example, in a Create React App setup:
// import ReactDOM from 'react-dom/client';
// const root = ReactDOM.createRoot(document.getElementById('root'));
// root.render(<App />);

view raw JSON →