useUser 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.
The hook returns metadata for a specified User ID, optionally including its custom data object.
User ID / UUID
User ID is also referred to as UUID
/uuid
in some APIs and server responses but holds the value of the userId
parameter you set during initialization.
This hook also sets up a listener that will react to updates of the fetched user. However, this behavior requires a living subscription to the user getting updated - this should be handled by the components.
This hook can be used in order not to fetch data about all users of the application. Instead, only the current user metadata is passed into the Chat
provider and the senderInfo
option is enabled on MessageInput
. This way the information about the senders is attached directly to the messages.
const [user, error, isLoading] = useUser({ uuid: "user" });
return (
<Chat users={[user]}>
<MessageInput senderInfo={true} />
</Chat>
);
Input
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
uuid | String | Optional | current User ID | Unique user identifier. If not supplied, the current user's User ID is used. |
include | Object | Optional | n/a | Option to include respective additional fields in the response. |
→ customFields | Boolean | Optional | false | Option to fetch custom fields. |
Output
Parameter | Type | Description |
---|---|---|
array[0] | User | Metadata of the user. |
array[1] | Error | If there's an error fetching the user, it will be available here. |
array[2] | Boolean | Indicator that the user data is still being loaded. |