Practice Problems
Create a reusable React component implementing Cookies. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Cookies using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Cookies for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What does the HttpOnly flag do?
2. What is the maximum size of a cookie?
3. Which SameSite value provides the strongest CSRF protection?
4. What is the difference between Max-Age and Expires?
Flashcards
Question
What are cookies?
Click to reveal answer
Answer
Small pieces of data stored in the browser, sent with every HTTP request to the same domain. Used for authentication, preferences, and tracking.
Question
What does Secure flag do?
Click to reveal answer
Answer
Ensures cookie is only sent over HTTPS connections, preventing interception over HTTP.
Question
What is the difference between session and persistent cookies?
Click to reveal answer
Answer
Session cookies are deleted when the browser closes. Persistent cookies have an expiry date and survive browser restarts.
Question
How do you delete a cookie?
Click to reveal answer
Answer
Set its value to empty and Max-Age to 0: document.cookie = 'name=; Max-Age=0; path=/'
Question
What is Cookies?
Click to reveal answer
Answer
Cookies is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.Cookies are sent with every HTTP request to the same domain
- 2.Use Secure, HttpOnly, and SameSite for security
- 3.Session cookies expire when browser closes, persistent cookies have expiry dates
- 4.SameSite attribute protects against CSRF attacks
- 5.Keep cookie sizes small to minimize request overhead
Interview Tips
- •Explain how cookies enable authentication
- •Know the security implications of each cookie flag
- •Understand SameSite and CSRF protection
- •Know when to use cookies vs localStorage vs sessions
Cheat Sheet
Cookies Cheat Sheet
Cookie Properties:
- Name/Value: Cookie data
- Domain: Which domains receive it
- Path: URL path scope
- Max-Age/Expires: Lifetime
- Secure: HTTPS only
- HttpOnly: No JS access
- SameSite: CSRF protection
Security Best Practices:
- Use Secure + HttpOnly for auth cookies
- Use SameSite=Strict for sensitive data
- Set short expiry for session cookies
- Always use HTTPS
SameSite Values:
- Strict: Never cross-site
- Lax: Top-level GET only (default)
- None: All requests (needs Secure)
Limitations:
- ~4KB per cookie
- ~50 per domain
- Sent with every request