Hooks for PubNub Chat Components for React
Migrate to Chat SDK
PubNub will stop supporting Chat Components on January 1, 2025 but you are welcome to contribute. Learn how to migrate to the Chat SDK here.
Hooks is a new API for React introduced in version 16.8 (released in February 2019). They allow for using state and other React features without writing classes.
The main motivation behind this new API was the ability of creating reusable logic, including state, and plugging it into React components. So the goal is similar to Higher-Order Components or render props patterns, but Hooks resolve a lot of the issues of its predecessors and are much simpler to use. Another big benefit is the possibility to put interconnected logic close to each other. In addition, components using Hooks result in much shorter code and provide a good developer experience.
usePubNub
from our React SDK is in fact a React hook, providing a PubNub instance into client components. The word “use” at the beginning of every hook is a name convention set by the React authors.
Custom chat hooks
Apart from the actual components, PubNub Chat Components for React come with a set of custom React hooks. The hooks were created to easily fetch the metadata stored within PubNub objects and pass it directly into the components.
You can skip this section if metadata is handled externally. Otherwise, you can use PubNub's infrastructure to store your data prior to, or during runtime of your application using our standard JavaScript SDK methods.