summaryrefslogtreecommitdiff
path: root/src/components/RsvpForm.tsx
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2024-03-27 18:24:55 -0700
committerMichael Hunteman <michael@huntm.net>2024-03-27 18:24:55 -0700
commitfc07a039bb79cc7647b7ba7edee46e4f019093e0 (patch)
tree6984c3a3457898c0b0f7193c16f0989b345f5542 /src/components/RsvpForm.tsx
parent32ade97a48d6ff2cc074d36038c4cdb89fc4fbb9 (diff)
Use ref instead of state for previous party size
Diffstat (limited to 'src/components/RsvpForm.tsx')
-rw-r--r--src/components/RsvpForm.tsx20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/components/RsvpForm.tsx b/src/components/RsvpForm.tsx
index a1e4fa8..a6e7036 100644
--- a/src/components/RsvpForm.tsx
+++ b/src/components/RsvpForm.tsx
@@ -12,7 +12,7 @@ import {
import { useForm, Controller, useFieldArray } from 'react-hook-form';
import { useOutletContext } from 'react-router-dom';
import { useUpdateGuestMutation, Guest } from '../apiSlice';
-import { useState } from 'react';
+import { useRef } from 'react';
type FormValues = {
id: number;
@@ -31,7 +31,7 @@ type FormValues = {
function RsvpForm() {
const [updateGuest] = useUpdateGuestMutation();
const guest: Guest = useOutletContext();
- const [previousPartySize, setPreviousPartySize] = useState(1);
+ const previousPartySize = useRef(0);
const {
register,
@@ -62,21 +62,25 @@ function RsvpForm() {
const handleParty = () => {
const partySize = Number(watch('partySize'));
- if (partySize > previousPartySize && partySize > 1 && partySize < 10) {
+ if (
+ partySize > previousPartySize.current &&
+ partySize > 1 &&
+ partySize < 10
+ ) {
append(
- new Array(partySize - previousPartySize).fill({
+ new Array(partySize - previousPartySize.current).fill({
firstName: '',
lastName: '',
})
);
- setPreviousPartySize(partySize);
- } else if (partySize < previousPartySize && partySize > 0) {
+ previousPartySize.current = partySize;
+ } else if (partySize < previousPartySize.current && partySize > 0) {
remove(
- [...Array(previousPartySize - partySize).keys()].map(
+ [...Array(previousPartySize.current - partySize).keys()].map(
(_, i) => partySize - 1 + i
)
);
- setPreviousPartySize(partySize);
+ previousPartySize.current = partySize;
}
};