You’re building a to-do list app in React. You write the following code to render the list, but the console shows a warning: "Each child in a list should have a unique 'key' prop."
const todos = ["Buy milk", "Call mom", "Finish homework"]; function ToDoList() { return ( <ul> {todos.map((todo) => <li>{todo}</li>)} </ul> ); }
What can you do to fix this warning?
Wrap the <li> in a <div> and add a key to the <div>.
<li>
<div>
key
Assign a random number as the key, like key={Math.random()}.
key={Math.random()}
Add a key prop using the array index, like key={index}.
key={index}
Ignore the warning since it doesn’t affect how the list renders.
Add a key prop using the todo text, like key={todo}.
key={todo}
Consider this code where a list of articles is displayed:
const articles = [ { id: 1, title: "React Basics" }, { id: 2, title: "Advanced JSX" } ]; function ArticleList({ articles }) { return ( <ul> {articles.map((article, index) => <li key={index}>{article.title}</li>)} </ul> ); }
A user adds a new article { id: 3, title: "Hooks Intro" } at the beginning of the list. What might go wrong?
{ id: 3, title: "Hooks Intro" }
The console will throw a "duplicate key" error.
React might not update the list efficiently because the keys (indices) shift, causing potential rendering bugs.
The new article won’t appear in the UI.
The list will render correctly with no issues since indices are unique during each render.
You’re tasked with improving this code to follow best practices:
const tasks = [ { id: "t1", desc: "Task 1" }, { id: "t2", desc: "Task 2" } ]; function TaskList({ tasks }) { return ( <ul> {tasks.map((task, index) => <li key={index}>{task.desc}</li>)} </ul> ); }
Which of the following changes align with React best practices?
Add a check for an empty list and return a message if tasks.length is 0.
tasks.length
Use key={task.desc} since descriptions are unique.
key={task.desc}
Remove the key prop since it’s optional.
Replace <li> with a custom <TaskItem> component to render each task.
<TaskItem>
Change the key to use task.id instead of index.
task.id
index
You encounter this code in a project:
function ItemList({ items }) { return ( <> {items.map((item) => <span>{item.name}</span>)} </> ); }
Why does this code fail, and how can you fix it?
It fails because <></> shorthand fragments don’t support keys; use <Fragment key={item.id}> instead.
<></>
<Fragment key={item.id}>
It works fine as is; no changes are needed.
It fails because <span> needs a key prop; add key={item.id} to the <span>.
<span>
key={item.id}
It fails because items might be undefined; add a check like items && items.map(...).
items
items && items.map(...)
Imagine you’re debugging this app:
const initialBooks = [ { id: 1, title: "Book A" }, { id: 2, title: "Book B" } ]; function BookList({ books }) { if (!books.length) { return <p>No books available.</p>; } return ( <ul> {books.map((book, index) => <li key={index}>{book.title}</li>)} </ul> ); } function App() { return <BookList books={initialBooks} />; }
A user deletes "Book A" (id: 1), leaving only "Book B" (id: 2). What’s the best way to improve this code based on the result?
Keep the code as is since it works for this simple case.
Use key={book.id} instead of key={index} to ensure stable keys.
key={book.id}
Add a loading state to handle async data fetching.
Replace the empty list check with a ternary operator for cleaner code.
This content is only available to registered users.Please register at the the home page.