Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

34 řádky
1.1 KiB

  1. ---
  2. description: Prevent double-click / duplicate API calls from frontend UI
  3. alwaysApply: true
  4. ---
  5. # Prevent duplicated API calls (frontend)
  6. When wiring UI actions (buttons, row actions, dialogs) to backend APIs, **always prevent double submission**. Relying on `setState` + `disabled` alone is not sufficient because rapid double-click can fire twice before React re-renders.
  7. - **Must**: add an **in-flight lock** (e.g. `useRef(false)`) and early-return if already running.
  8. - **Must**: keep the UI disabled/loading (`disabled={isLoading}`) for user feedback.
  9. - **Must**: clear the lock in `finally` so it always releases.
  10. - **Should**: if the same endpoint can be triggered from multiple places, consider a shared “single-flight” helper (dedupe by `method+url+body` key).
  11. Example pattern:
  12. ```tsx
  13. const inFlightRef = useRef(false);
  14. const [isSaving, setIsSaving] = useState(false);
  15. const onSave = async () => {
  16. if (inFlightRef.current) return;
  17. inFlightRef.current = true;
  18. setIsSaving(true);
  19. try {
  20. await doRequest();
  21. } finally {
  22. setIsSaving(false);
  23. inFlightRef.current = false;
  24. }
  25. };
  26. ```