#Disable button while processing. I need to do that to many buttons. Easy way exists?

19 messages ยท Page 1 of 1 (latest)

smoky thicket
#

In my app, I got 10+ buttons, which I want to be disabled while it's processing.
I could use const [buttonOneProcessing, setButtonOneProcessing] = useState(false), and apply it +10x to all the various buttons.
But I wonder, is there a faster easier way?

ornate plankBOT
#

๐Ÿ”Ž This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

๐Ÿ•ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

โœ… You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

warm idol
torn zenith
#

A fieldset that is marked as disabled will disable all its children. So you could use that to propagate the state to buttons.

smoky thicket
#

Actually, those buttons are in different places and at diff times

#

Any other ideas?

#

I know that jQuery have .one() https://www.w3schools.com/jquery/event_one.asp , to prevent multiple submissions once a button is pressed for processing. I wonder if something similar exists in react

warm idol
# smoky thicket Actually, those buttons are in different places and at diff times

then you can also use one loading state and every button uses it. Like:

const [isLoading, setIsLoading] = useState(false)

return (
  <>
  <LoadingButton isLoading={isLoading}>First button</LoadingButton>
<LoadingButton isLoading={isLoading}>Second button</LoadingButton>
</>
)

Another approach would be if you use the buttons inside forms. Then you can use the useActionState. It contains an "isPending" parameter that will be updated when there is a pending Transition

sullen drift
#

I'm a bit of a noob but could you not use something like react context for the buttons and have the loading state there?

warm idol
sullen drift
#

I was having similar issues the other day with something and went for context for it, anyone here know how I can send invites from my app? I've tried sendgrid but I got skill issues 7529_KEKW

warm idol
sullen drift
#

Ok thank you. Gonna open my own thread, I'm still new to next js and never done this type of app before

sullen drift
warm idol
smoky thicket
#

Yea, I heard of the new useActionState hook`. Lemme explore if it can help me here

warm idol
ornate plankBOT