PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

react

  • Getting Started
  • API Reference

    • PubNubConsumer
    • usePubNub
    • PubNubProvider
  • Change Log
  • Platform Support
  • Getting Started React Native

usePubNub Hook

The usePubNub hook lets you interact with PubNub in function components.

Hooks are a new feature added in React 16.8 that allow you to use React features without writing a class. For a general overview of hooks, refer to the React documentation.

Example

import React from 'react';
import { usePubNub } from '../../src/index';

const PubNubTime = () => {
  const PubNubClient = usePubNub();

  return (
    <div>
      const [time, setTime] = useState("Loading");

      PubNubClient.time((status, response) => setTime(response.timetoken));

      {time}
    </div>
  );
};

export default PubNubTime;
← PubNubConsumerPubNubProvider →
  • Example
© PubNub Inc. - Privacy Policy