📊
Pollz
  • Introduction
  • Quickstart
  • Guides
    • Initializing your app
    • Create a Poll
    • Vote for a Poll
    • Retrieve a Poll
    • Manage Poll Options
    • Listen to update
    • Anonymous Poll
  • Components
    • React
    • React Native
    • Embedded
  • API Reference
    • init()
    • pollOptions()
      • addOption()
      • deleteOption()
      • renameOption()
    • anonymous
      • createAnonymousToken()
      • getAnonymousPoll()
      • voteAnonymously()
    • polls
      • create()
      • get()
      • getAll()
      • vote()
      • rename()
      • deleteOne()
      • listen()
    • pollTypes
      • getAll()
  • Tooling
    • Typescript
    • React
      • usePoll()
      • usePolls()
      • usePollz()
      • usePollTypes()
      • useAnonymousPoll()
Powered by GitBook
On this page
  • usePoll
  • Parameters
  • Return Values
  • Example
  1. Tooling
  2. React

usePoll()

usePoll

The usePoll hook is a custom hook provided by the pollz-react package. It allows you to fetch and manage real-time updates for a specific poll from the Pollz SDK within a React component.

Parameters

pollId (EntryIdType)

  • The unique identifier of the poll you want to fetch and listen to updates for.

options (optional)

An object with the following optional properties:

  • listen (boolean): Whether to set up a real-time listener for updates. Default is false.

  • orderOptionsBy (OrderBy): The order in which to retrieve poll options for the updated poll. Default is OrderBy.Asc (ascending).

Return Values

The usePoll hook returns an object with the following properties:

  • poll (PollWithOptions | null): The fetched poll object. null if the poll is still loading.

  • refetch (function): A function to manually refetch the poll data.

Example

const YourComponent = ({ pollId }) => {
  const { poll, refetch } = usePoll(pollId, { listen: true, orderOptionsBy: OrderBy.Asc });

  if (!poll) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h2>{poll.name}</h2>
      {/* Render your poll component with poll data */}
      <button onClick={() => refetch()}>Refetch Poll</button>
    </div>
  );
};

The usePoll hook provides an easy way to integrate specific poll data into your React components, with support for real-time updates. Customize the hook based on your application's needs.

PreviousReactNextusePolls()

Last updated 1 year ago