React

The package pollz-react has been develop to integrate the Node.js SDK easily within a React app.

npm install pollz-react
yarn add pollz-react

The PollzProvider component wraps your React application, providing access to the Pollz SDK functionalities through React context. It initializes the SDK with the specified appId and appSecret and makes the SDK instance available to all child components.

useAnonymousPoll

  • Fetches an anonymous poll using the provided pollToken.

  • Returns the poll object and a function to refetch the poll.

usePollTypes

  • Fetches all available poll types.

  • Returns the list of poll types.

usePoll

  • Fetches a specific poll using the provided pollId.

  • Optionally listens for real-time updates on the poll.

  • Returns the poll object and a function to refetch the poll.

usePolls

  • Fetches a paginated list of all polls.

  • Returns the list of polls and a function to refetch the list.

usePollz

  • Retrieves the Pollz SDK instance and initialization status.

  • Ensures that the component is wrapped within the PollzProvider.

Usage Example:

import React from 'react';
import { PollzProvider, usePolls, usePollz } from 'pollz-react';

const App = () => {
  const { polls, refetch: refetchPolls } = usePolls();
  const { initialized } = usePollz();

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

  return (
    <div>
      <h1>Pollz React App</h1>
      <button onClick={() => refetchPolls()}>Refetch Polls</button>
      {/* Render your components using the fetched data */}
    </div>
  );
};

const PollzApp = () => (
  <PollzProvider appId="your-app-id" appSecret="your-app-secret">
    <App />
  </PollzProvider>
);

export default PollzApp;

This package provides a clean and convenient way for React developers to integrate Pollz functionality into their applications. If you have specific questions or need further assistance, feel free to let me know!

Last updated