Sortable List
React.js
medium
mins
Create a React component called SortableList that allows users to add new items to a list and sort the list either in ascending or descending order. The sorting action should be optimized using the useCallback hook to avoid unnecessary re-renders of the sorting function when the list or the order is updated.
You need to:
- Implement an input field with the placeholder "Add a new item".
- A button labelled "Add Item" to add new items to the list.
- Ensure each item in the list has a unique id in the format item-<index>.
- Implement two buttons labelled "Sort Ascending" and "Sort Descending" to sort the list in ascending or descending order.
- Use JavaScript’s built-in .sort() method to sort the list items alphabetically.
- Use
useCallbackto memoize the sorting function to avoid unnecessary re-creation of the function during re-renders. - The list should dynamically update after each operation (adding items or sorting).
Reference UI

Preview what you need to build
Companies:
airbnb
paypal
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
