DEV Community

Cover image for How to Build a Live Poll in React in Under 5 Minutes (Without a Backend)
Adriano Raiano
Adriano Raiano

Posted on • Originally published at vaultrice.com

How to Build a Live Poll in React in Under 5 Minutes (Without a Backend)

Ever wanted to get quick feedback from your users right inside your app? A live poll is perfect, but the thought of building the backend for it — setting up a database, a real-time API with WebSockets, and handling the logic — can turn a simple idea into a week-long project.

But what if you could do it in minutes, with just a frontend component?

Today, I'll show you how to add a fully real-time, persistent poll to any React app. When one user votes, the results will instantly update for everyone else, everywhere.

Here’s what we’ll build:

Prerequisites

  • A basic React application (e.g., created with create-react-app or vite).
  • About 5 minutes of your time.

Let's dive in.


Step 1: The "Backend" (in 60 seconds)

First, we need a place to store our poll data. We'll use Vaultrice, a real-time component backend for frontend developers.

  1. Create a free account at www.vaultrice.app/register.
  2. Create a new Project (e.g., "My React App").
  3. Go to the API Keys section and create a new key.
  4. Copy your projectId, apiKey, and apiSecret.

That’s it. That’s the entire backend setup.


Step 2: Install the React Component

Next, we'll install the pre-built Vaultrice React component for our poll.

Open your terminal and run:

npm install @vaultrice/react-components
Enter fullscreen mode Exit fullscreen mode

Step 3: Add the Poll to Your App

Now for the magic. Import the <Voting /> component and drop it into your React app. Pass in the credentials you just copied.

import { Voting } from '@vaultrice/react-components';

function App() {
  const credentials = {
    projectId: 'YOUR_PROJECT_ID',
    apiKey: 'YOUR_API_KEY',
    apiSecret: 'YOUR_API_SECRET'
  };

  return (
    <div className="container">
      <Voting
        id="my-first-poll"
        title="Do you like to code?"
        choices={[
          { id: 'yes', label: 'Yes' },
          { id: 'sometimes', label: 'Sometimes' },
          { id: 'no', label: 'No' },
        ]}
        credentials={credentials}
      />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

And... you're done.

You now have a fully functional, real-time poll. When a user votes, the component uses Vaultrice's atomic counters to safely increment the vote count, and the results are broadcast to all other connected clients instantly.

How Does It Work?

The <Voting /> component (of @vaultrice/react-components) is powered by the @vaultrice/sdk and the @vaultrice/react, which handles all the complexity of real-time communication and state management for you.

  • Atomic Counters: It uses incrementItem() to ensure every vote is accurately counted without race conditions.
  • Real-Time Sync: It automatically subscribes to data changes, so the UI updates live for everyone.
  • Zero Backend Code: You didn't have to write a single line of server code, configure a database, or manage a WebSocket connection.

Conclusion

This is just a small example of what you can build. The same principles apply to creating "who's online" lists, real-time comment sections, collaborative features, and more.

If you're tired of the backend hassle for real-time features, give Vaultrice a try.

Top comments (1)

Collapse
 
dev-rashedin profile image
Rashedin | FullStack Developer

Wow, this is incredibly simple to use! I’ll definitely give it a try. I’m especially curious about how it works under the hood — how you managed to make it this straightforward. It would be amazing if real-time chat could be this simple too!