1
2
3
4
5
6
Use the "Verify Solution" button to get AI feedback on your React code.
You are tasked with building a dynamic list of counters. Each counter should have its own state (value) and buttons to increment and decrement. Additionally, there should be functionality to add new counters and reset all existing counters to zero. The primary challenge is to ensure optimal performance by preventing unnecessary re-renders of individual Counter components and their associated callback functions, especially when the list grows large. You must leverage React's useReducer hook for managing the list state and React.memo and useCallback for performance optimizations.
Your goal is to:
counterListReducer: Implement the ADD_COUNTER action to correctly add a new counter with a unique ID and initial value.Counter component: Ensure individual Counter components only re-render when their id or value props change.handleIncrement and handleDecrement from being recreated on every App render, which would break the memoization of the Counter components.