React StrictMode Exposes Hidden Bug in Exam Submission Code

🚨 My exam platform was submitting answers… before users even touched anything. No clicks. No interaction. Just open the page → 💥 SUBMIT. I thought it was: - API issue ❌ - State bug ❌ - Logic mistake ❌ But the real reason surprised me: ⚛️ React StrictMode. In React 18 (development), components run twice to detect side effects. So this simple code: useEffect(() => { submitExam(); }, []); Was actually running twice… triggering duplicate submission. 💡 The fix? Control the side effect using a guard: const hasSubmitted = useRef(false); useEffect(() => { if (hasSubmitted.current) return; hasSubmitted.current = true; submitExam(); }, []); 🔥 The lesson: StrictMode didn’t break my app… It exposed a hidden bug I didn’t even know existed. And honestly? That bug could have been dangerous in real-world scenarios like: - Payments 💳 - Orders 🛒 - Exam submissions 📝 💬 Build carefully. Because sometimes… React tests your code more than your users do. #React #JavaScript #Frontend #WebDevelopment #Debugging #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories