React SDK API reference
In this section, you'll find the complete documentation for the components exposed in @knocklabs/react
, including the props available.
Note: You can see a reference for the methods available for the Knock
class, as well as a Feed
instance under the client JS docs.
Context
KnockProvider
The top-level provider that connects to Knock with the given API key and authenticates a user.
Props
Accepts KnockProviderProps
useKnockClient
The KnockProvider
exposes a useKnockClient
hook for all child components.
Returns: Knock
, an instance of the Knock JS client.
Example:
1import { KnockProvider, useKnockClient } from "@knocklabs/react";
2
3const App = ({ authenticatedUser }) => (
4 <KnockProvider
5 apiKey={process.env.KNOCK_PUBLIC_API_KEY}
6 userId={authenticatedUser.id}
7 >
8 <MyComponent />
9 </KnockProvider>
10);
11
12const MyComponent = () => {
13 const knock = useKnockClient();
14
15 return null;
16};
KnockFeedProvider
The feed-specific provider that connects to a feed for that user. Must be a child of the KnockProvider
.
Props
Accepts KnockFeedProviderProps
:
useKnockFeed
The KnockFeedProvider
exposes a useKnockFeed
hook for all child components.
Returns: KnockFeedProviderState
Example:
1import {
2 KnockProvider,
3 KnockFeedProvider,
4 useKnockFeed,
5} from "@knocklabs/react";
6
7const App = ({ authenticatedUser }) => (
8 <KnockProvider
9 apiKey={process.env.KNOCK_PUBLIC_API_KEY}
10 userId={authenticatedUser.id}
11 >
12 <KnockFeedProvider feedId={process.env.KNOCK_FEED_CHANNEL_ID}>
13 <MyFeedComponent />
14 </KnockFeedProvider>
15 </KnockProvider>
16);
17
18const MyFeedComponent = () => {
19 const { useFeedStore } = useKnockFeed();
20 const items = useFeedStore((state) => state.items);
21
22 return (
23 <div className="notifications">
24 {items.map((item) => (
25 <NotificationCell key={item.id} item={item} />
26 ))}
27 </div>
28 );
29};
KnockInAppMessagesChannelProvider
Props
useInAppMessagesChannel
Only available under the KnockInAppMessagesChannelProvider
.
Returns:
KnockSlackProvider
The Slack-specific provider that connects to a Slack workspace for the given tenant. Must be a child of the KnockProvider
.
Props
Accepts KnockSlackProviderProps
KnockMsTeamsProvider
The Microsoft Teams-specific provider that connects to Microsoft Teams for the given tenant. Must be a child of the KnockProvider
.
Props
Accepts KnockMsTeamsProviderProps
useKnockSlackClient
The KnockSlackProvider
exposes a useKnockSlackClient
hook for all child components.
Returns:
Example:
1import {
2 KnockProvider,
3 KnockSlackProvider,
4 useKnockSlackClient,
5} from "@knocklabs/react-core";
6
7const App = ({ authenticatedUser, tenant }) => (
8 <KnockProvider
9 apiKey={process.env.KNOCK_PUBLIC_API_KEY}
10 userId={authenticatedUser.id}
11 >
12 <KnockSlackProvider knockSlackChannelId={process.env.KNOCK_SLACK_CHANNEL_ID} tenant={tenant}>
13 <MySlackComponent />
14 </KnockFeedProvider>
15 </KnockProvider>
16);
17
18const MySlackComponent = () => {
19 const { connectionStatus,
20 setConnectionStatus,
21 errorLabel,
22 setErrorLabel,
23 actionLabel,
24 setActionLabel,
25 knockSlackChannelId,
26 tenant, } =
27 useKnockSlackClient();
28
29 return (
30 <div className="slack-auth-status">
31 <AuthStatus connectionStatus={connectionStatus} />
32 </div>
33 );
34};
General components
Button
A generic Button component that can be used to add action buttons to items in a notification feed.
Props
ButtonGroup
Used to group and space multiple Button
components into a single line.
Props
KnockI18nProvider
A provider to inject translations into components.
Props
Feed components
NotificationFeed
Props
NotificationFeedPopover
Renders a NotificationFeed
in a floating popover, rendered by popper-js
.
Props
Accepts the same base props as NotificationFeed
, and overrides with the following:
NotificationCell
Props
NotificationIconButton
Renders a notification bell icon, with a badge showing the number of unseen items present in the notification feed.
Props
Messaging components
By default, our React library exposes 3 different out of the box components: Banner
, Card
, and Modal
. Each component maps to a message type that Knock ships.
Each of the components exports a *View
component that contains granular view components that can be composed to override specific parts of the pre-built components.
Banner
Props
Example
1import { Banner } from "@knocklabs/react";
2
3<Banner filters={{ tenant_id: "tenant-1" }} />;
Card
Props
Example
1import { Card } from "@knocklabs/react";
2
3<Card filters={{ tenant_id: "tenant-1" }} />;
Modal
Props
Example
1import { Modal } from "@knocklabs/react";
2
3<Modal filters={{ tenant_id: "tenant-1" }} />;
Slack components
SlackAuthButton
Props
SlackAuthContainer
Props
SlackChannelCombobox
Props
Microsoft Teams components
MsTeamsAuthButton
Props
MsTeamsAuthContainer
Props
MsTeamsChannelCombobox
Props
General hooks
useAuthenticatedKnockClient
Creates an authenticated Knock client.
Params
Returns
Knock
instance, authenticated against the user
Example
1import { useAuthenticatedKnockClient } from "@knocklabs/react";
2
3const MyComponent = () => {
4 const knock = useAuthenticatedKnockClient(
5 process.env.KNOCK_PUBLIC_API_KEY,
6 user.id,
7 user.knockToken,
8 );
9
10 return null;
11};
useTranslations
Exposed under KnockI18nProvider
child components.
Returns:
Feed hooks
useNotifications
Creates a Feed
instance for the provided Knock
client which creates a stateful, real-time connection to Knock to build in-app experiences.
Params
Returns
Feed
instance
Example
1import {
2 useAuthenticatedKnockClient,
3 useNotifications,
4 useNotificationStore,
5} from "@knocklabs/react";
6
7const MyComponent = () => {
8 const knock = useAuthenticatedKnockClient(
9 process.env.KNOCK_PUBLIC_API_KEY,
10 user.id,
11 user.knockToken,
12 );
13
14 const notificationFeed = useNotifications(
15 knock,
16 process.env.KNOCK_FEED_CHANNEL_ID,
17 );
18
19 const { metadata } = useNotificationStore(notificationFeed);
20
21 useEffect(() => {
22 notificationFeed.fetch();
23 }, [notificationFeed]);
24
25 return <span>Total unread: {metadata.unread_count}</span>;
26};
useNotificationStore
Given a Feed
will return reactive access to the FeedStateStore
. Can optionally accept a selector
to retrieve a fine-grained slice of state from the store.
Params
Returns
FeedStoreState
Example
1import { useNotificationStore } from "@knocklabs/react";
2
3const MyComponent = () => {
4 // Fetch everything
5 const state = useNotificationStore(feedClient);
6
7 // Only select the metadata
8 const metadata = useNotificationStore(feedClient, (state) => state.metadata);
9
10 // Filter for only unread items
11 const unreadItems = useNotificationStore(feedClient, (s) =>
12 s.items.filter((item) => !item.read_at),
13 );
14};
Messaging hooks
useInAppMessages
A hook used to fetch in-app messages for a particular user, channel, message type triple.
Props:
Returns:
Example:
1import { useInAppMessage } from "@knocklabs/react";
2
3const MyMessage = () => {
4 const { message, inAppMessagesClient } = useInAppMessage("type", {
5 tenant_id: "tenant-1",
6 });
7
8 if (!message) return null;
9
10 return <div>Fetched message: {message.id}</div>;
11};
Slack hooks
Exposed under KnockSlackProvider
child components.
useSlackAuth
Builds a Slack authorization URL generator and a disconnect function.
Props:
Returns:
useSlackChannels
This hook will continually fetch partial data about Slack channels from a given workspace up to the max limit given.
Props:
Returns:
useConnectedSlackChannels
This hook returns partial data about the Slack channels that are present on the given recipient object's channel data. These are the Slack channels that would be notified with this object as a recipient of a workflow.
Props:
Returns:
Microsoft Teams hooks
Exposed under KnockMsTeamsProvider
child components.
useMsTeamsAuth
Builds a Microsoft Teams authorization URL generator and a disconnect function.
Props:
Returns:
useMsTeamsTeams
This hook will continually fetch partial data about Microsoft Teams teams within a given Microsoft Entra tenant up to the max limit given.
Props:
Returns:
useMsTeamsChannels
This hook will continually fetch partial data about Microsoft Teams channels within a given team.
Props:
Returns:
useConnectedMsTeamsChannels
This hook returns partial data about the Microsoft Teams channels that are present on the given recipient object's channel data. These are the Microsoft Teams channels that would be notified with this object as a recipient of a workflow.
Props:
Returns:
Types
I18nContent
Used to set translations available in the child components exposed under KnockFeedProvider
, KnockSlackProvider
, and KnockMsTeamsProvider
. Used in the useTranslations
hook.
Note: locale
must be a valid locale code.
1interface Translations {
2 readonly emptyFeedTitle: string;
3 readonly emptyFeedBody: string;
4 readonly notifications: string;
5 readonly poweredBy: string;
6 readonly markAllAsRead: string;
7 readonly archiveNotification: string;
8 readonly all: string;
9 readonly unread: string;
10 readonly read: string;
11 readonly unseen: string;
12
13 readonly msTeamsChannelSetError: string;
14 readonly msTeamsConnect: string;
15 readonly msTeamsConnected: string;
16 readonly msTeamsConnecting: string;
17 readonly msTeamsConnectionErrorExists: string;
18 readonly msTeamsConnectionErrorOccurred: string;
19 readonly msTeamsConnectContainerDescription: string;
20 readonly msTeamsDisconnect: string;
21 readonly msTeamsDisconnecting: string;
22 readonly msTeamsError: string;
23 readonly msTeamsReconnect: string;
24 readonly msTeamsTenantIdNotSet: string;
25
26 readonly slackConnectChannel: string;
27 readonly slackChannelId: string;
28 readonly slackConnecting: string;
29 readonly slackDisconnecting: string;
30 readonly slackConnect: string;
31 readonly slackConnected: string;
32 readonly slackConnectContainerDescription: string;
33 readonly slackSearchbarDisconnected: string;
34 readonly slackSearchbarNoChannelsConnected: string;
35 readonly slackSearchbarNoChannelsFound: string;
36 readonly slackSearchbarChannelsError: string;
37 readonly slackSearchChannels: string;
38 readonly slackConnectionErrorOccurred: string;
39 readonly slackConnectionErrorExists: string;
40 readonly slackChannelAlreadyConnected: string;
41 readonly slackError: string;
42 readonly slackDisconnect: string;
43 readonly slackChannelSetError: string;
44 readonly slackAccessTokenNotSet: string;
45 readonly slackReconnect: string;
46}
47
48interface I18nContent {
49 readonly translations: Partial<Translations>;
50 readonly locale: string;
51}
SlackChannelQueryOptions
1type SlackChannelQueryOptions = {
2 maxCount?: number; // The max number of channels to return; default: 1000
3 limitPerPage?: number; // How many Slack channels will be returned per request; default: 200
4 excludeArchived?: boolean; // Whether to include archived channels; default: true
5 types?: string; // Types of channels to return; default: "private_channel,public_channel"
6 teamId?: string; // Filters channels to a specific team ID; default: null
7};
SlackChannel
1type SlackChannel = {
2 name: string;
3 id: string;
4 is_private: boolean;
5 is_im: boolean;
6 context_team_id: boolean;
7};
SlackChannelConnection
1type SlackChannelConnection = {
2 access_token?: string;
3 channel_id?: string;
4 incoming_webhook?: string;
5 user_id?: null;
6};
MsTeamsTeamQueryOptions
1type MsTeamsTeamQueryOptions = {
2 maxCount?: number; // The max number of teams to return; default: 1000
3 limitPerPage?: number; // How many teams will be returned per request; default: 100
4 filter?: string; // OData $filter query param to filter teams; default: null
5 select?: string; // OData $select query param to select fields; default: "id,displayName"
6};
MsTeamsChannelQueryOptions
1type MsTeamsChannelQueryOptions = {
2 filter?: string; // OData $filter query param to filter channels; default: "isArchived eq false and membershipType eq 'standard'"
3 select?: string; // OData $select query param to select fields; default: "id,displayName"
4};
MsTeamsTeam
1type MsTeamsTeam = {
2 id: string;
3 displayName: string;
4 description?: string;
5};
MsTeamsChannel
1type MsTeamsChannel = {
2 id: string;
3 displayName: string;
4 description?: string;
5 membershipType?: string;
6 isArchived?: boolean;
7 createdDateTime?: string;
8};
MsTeamsChannelConnection
1type MsTeamsChannelConnection = {
2 ms_teams_tenant_id?: string;
3 ms_teams_team_id?: string;
4 ms_teams_channel_id?: string;
5 ms_teams_user_id?: null;
6 incoming_webhook?: {
7 url: string;
8 };
9};
RecipientObject
1type RecipientObject = {
2 objectId: string;
3 collection: string;
4};
ConnectionStatus
1type ConnectionStatus =
2 | "connecting"
3 | "connected"
4 | "disconnected"
5 | "error"
6 | "disconnecting";