Building notification feeds with React

How to build an in-app notification feed using the drop-in React powered UI element.

Our @knocklabs/react library comes pre-built with a real-time feed component that you can drop into your application. In this guide, you'll find common recipes to help you work with the pre-built Knock feed UI element.

See a live demo of our pre-built in-app feed UI element ->

Getting started

To use this example, you'll need an account on Knock, as well as an in-app feed channel with a workflow that produces in-app feed messages. You'll also need:

  • A public API key for the Knock environment (set as KNOCK_PUBLIC_API_KEY)
  • The channel ID for the in-app feed (set as KNOCK_FEED_CHANNEL_ID)
🌠
Find your channel ID. To find the channel ID for your in-app channel(s), go to Integrations > Channels in the Knock dashboard, navigate to the channel page of your in-app channel, and copy the channel ID.

Installing dependencies

Rendering a notification feed (prebuilt UI element)

To add a real-time notifications feed to your product, you can use the out-of-the-box components that the @knocklabs/react library offers.

🔐
Secure your feed: By default, Knock feeds are accessible to anyone who has the feed ID. This makes it easy to get started in development. To secure your feed for production, enable enhanced security mode in your Knock dashboard and pass a signed userToken as a prop to the KnockFeedProvider component. For more information, visit the security & authentication guidefor client-side applications.

Common recipes

Building custom UI

You can build headless notifications UI on top of our hooks. Learn more.

Setting an action_url on the notification cell

You can pass a value in the Action URL field of your notification template and the KnockFeedProvider will attach a redirect handler if action_url is not empty.

Adding an onClick handler to the notification cell

You can customize the click handler for the notification cell as follows:

Both the NotificationFeed and the NotificationFeedPopover take a onNotificationClick prop.

Add an onButtonClick handler to the notification cell

You can add a button click handler to respond to clicks on the buttons in the notification cell. The onNotificationButtonClick prop is available on the NotificationFeed component:

Rendering a different notification cell in the feed

You can customize the rendering of a notification cell in the feed by overriding the renderItem prop.

Overriding the avatar

You can customize the Avatar component rendered within the NotificationCell by passing a different avatar component into the NotificationCell in the renderItem function passed to the feed:

Using read instead of seen for the badge count

Marking messages as read on the popover opening

Using dark mode

The feed supports an optional colorMode prop, that defaults to light but can be set as dark for dark mode support.

Rendering custom components in a notification cell

For more advanced use cases, you may need to render custom components as part of the notification cell, but don't need to override the cell entirely. For this use case, you can use the children prop to render custom components inside the notification cell.

Customizing the feed styling

The complete theme that controls the look and feel of the feed components can be customized for theme in a few different ways:

  1. Customizing the CSS variables. The feed is written using CSS variables, which you can override to better match the look and feel of your product. The best way to see the possible CSS variables in use is to look at the theme.css source code.

  2. Bringing your own CSS. It's possible to override the Knock CSS entirely to customize the look and feel of the feed. All of the feed classes are unique and start with rnf-. You can see the classes in use by looking at the source code of the components.

Setting custom translations

You can set custom translations for the components inside the feed by passing the i18n property to the KnockProvider component. You can provide a partial or full set of translations to be used following the expected I18nContent type.

Note: the default locale used in the components will be en. No other translations are provided out-of-the-box.

Filtering/scoping a feed

A feed can be scoped by any of the parameters that are accepted on the feed endpoint via the FeedClientOptions set in the defaultFeedOptions for the KnockFeedProvider component, or via the useNotifications hook.

You can read more in this guide on feed filtering.