React Assignment – 5

Basic Questions

  1. Replace useState with useReducer for a counter app with INCREMENT and DECREMENT actions.
  2. Write a reducer function that handles RESET action.
  3. Compare a useReducer counter vs a useState counter in the same component.
  4. Create a reducer that manages a toggle boolean (TOGGLE).
  5. Use dispatch({type: “INCREMENT”}) from a button click.
  6. Show initial state setup with useReducer({count: 0}).
  7. Access a DOM input field using useRef and console.log its value on button click.
  8. Set focus on an input field using ref.current.focus().
  9. Store a timer setInterval ID in useRef and stop it with clearInterval.
  10. Use useRef to count the number of renders without re-rendering.
  11. Persist a value across renders without triggering re-render.
  12. Store the previous count value using useRef.
  13. Memoize an expensive calculation (for loop up to 1e7) with useMemo.
  14. Compare expensive calculation with and without useMemo.
  15. Use dependency array in useMemo to recalculate only when needed.
  16. Memoize a derived value like sortedList based on a state array.
  17. Pass a callback (handleClick) as prop to child component without useCallback and observe re-renders.
  18. Optimize same callback with useCallback and confirm fewer re-renders.
  19. Compare useCallback with useMemo by memoizing a function vs value.
  20. Write a memoized event handler (useCallback) with dependency [count].

Intermediate Questions

  1. Create a reducer to manage a todo list (ADD_TODO, REMOVE_TODO).
  2. Add an action TOGGLE_TODO to mark todos completed.
  3. Manage a form state with multiple fields (name, email) using useReducer.
  4. Implement lazy initialization in useReducer with an initializer function.
  5. Create a cart state with ADD_ITEM, REMOVE_ITEM.
  6. Compare code complexity of cart implemented with useState vs useReducer.
  7. Use useRef to handle video play/pause control in a component.
  8. Store a previous state value with useRef for comparison.
  9. Handle focus between two input fields (focus moves from one input to another using ref).
  10. Use useRef to persist scroll position across re-renders.
  11. Animate an element by changing its style using ref.current.
  12. Use useMemo to memoize a filtered list (filter items based on input).
  13. Optimize rendering of a large list with a derived filtered value using useMemo.
  14. Benchmark performance difference of list filtering with vs without useMemo.
  15. Combine useMemo with React.memo child component for optimization.
  16. Memoize a calculation that depends on multiple states.
  17. Use useCallback inside a parent so child doesn’t unnecessarily re-render.
  18. Compare two components: one with anonymous inline handler vs useCallback.
  19. Use useCallback with React.memo together for preventing prop change re-renders.
  20. Break down how useCallback improves performance in a counter with multiple buttons.

Advanced Questions

  1. Build a complex form with useReducer, managing multiple fields, validations, and submit/reset actions.
  2. Implement a state machine with useReducer (e.g., login states: idle, loading, success, error).
  3. Manage nested state updates (blog posts with comments) immutably with useReducer.
  4. Build a stopwatch app using useRef (store timer ID) + state for elapsed time.
  5. Implement a debounced search using useRef to manage timer across renders.
  6. Build a large table that sorts with useMemo and re-renders rows efficiently using useCallback.
  7. Implement a search + filter system where filtering uses useMemo and event handlers are optimized with useCallback.
  8. Compare performance in React Profiler with and without useMemo + useCallback.
  9. Build a Todo App with filtering & memoization:
  • useReducer for todos
  • useMemo for filtered lists
  • useCallback for event handlers
  1. Create a full-feature counter dashboard:
  • useReducer for counter logic
  • useRef for tracking render count and timers
  • useMemo for derived total
  • useCallback for buttons handler optimization.