Skip to content

Instantly share code, notes, and snippets.

@PaulieScanlon
Created October 10, 2022 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulieScanlon/26ebbeb0b0885a8605fb38fe5538a0a7 to your computer and use it in GitHub Desktop.
Save PaulieScanlon/26ebbeb0b0885a8605fb38fe5538a0a7 to your computer and use it in GitHub Desktop.
Adding the "click" handler
import React, { useState } from 'react';
...
const Page = () => {
...
+ const handleClick = async (id) => {
+ setIsSubmitting(true);
+ try {
+ const response = await fetch(`/api/create-vote?id=${id}`);
+ if (!response.ok) {
+ throw new Error(response.statusText);
+ }
+ const result = await response.json();
+ setResults(result);
+ setIsSubmitting(false);
+ setHasVoted(true);
+ } catch (error) {
+ setIsSubmitting(false);
+ setError({
+ error: true,
+ message: error.message
+ });
+ }
+ };
return null;
};
export default Page;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment