[{"id":"reactint-001","courseId":"react-intermediate","domain":"components-jsx","topic":"Forwarding props with spread","prompt":"In review, almost every component in a codebase passes {...props} straight to a child, and it is hard to tell which props anything uses. The author says spread is the React way to forward props. What does the React guidance say?","hint":"Concise is not always clearer.","source":"https://react.dev/learn/passing-props-to-a-component#forwarding-props-with-the-jsx-spread-syntax","interaction":{"type":"single"},"options":[{"id":"a","text":"Spread is always preferred, since listing props one by one is a code smell","whyNot":"The docs say repetitive prop lists are fine and can be more legible."},{"id":"b","text":"Spread is fine where a component only forwards props, but using it everywhere often means you should split components and pass JSX as children","why":"The docs call for spread with restraint and name splitting components and passing children as the usual fix."},{"id":"c","text":"Spread syntax in JSX is deprecated in React 19 and should be replaced by forwardRef","whyNot":"Spread is plain JSX and is not deprecated; forwardRef is about refs."},{"id":"d","text":"Spreading props makes React skip prop comparison, so memo stops working","whyNot":"Spread just builds the same props object; memo compares it as usual."}],"answer":"b","explanation":"Forwarding all props with spread is fine for thin wrappers, but heavy use usually signals that components should be split and take children.","concept":"Forwarding all props with spread is fine for thin wrappers, but heavy use usually signals that components should be split and take children.","rev":1,"kind":"scenario","code":"function Card(props) {\n  return <Avatar {...props} />;\n}","takeaways":["Use {...props} with restraint; everywhere means your components want children."]},{"id":"reactint-002","courseId":"react-intermediate","domain":"components-jsx","topic":"Where the key goes","prompt":"A developer extracts a list row into TodoRow and moves key={todo.id} onto the li inside it, as below. The console now warns that each child in a list should have a unique key. What is the fix?","hint":"React reads keys where the array of elements is built.","source":"https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key","interaction":{"type":"single"},"options":[{"id":"a","text":"Use key={index} on the li, since the inner element needs a positional key","whyNot":"The key is still in the wrong place, and an index key breaks on reorder."},{"id":"b","text":"Read props.key inside TodoRow and pass it to the li","whyNot":"Components never receive key as a prop; pass a separate prop if the id is needed."},{"id":"c","text":"Put key={todo.id} on <TodoRow> in the map call","why":"Elements directly inside map() need the key, because it is used in the context of the surrounding array."},{"id":"d","text":"Wrap the li in a keyed Fragment inside TodoRow","whyNot":"That key is still inside the child, not on the element the array holds."}],"answer":"c","explanation":"Keys belong on the elements returned directly from map(), not on the root of the component those elements render.","concept":"Keys belong on the elements returned directly from map(), not on the root of the component those elements render.","rev":1,"kind":"scenario","code":"function TodoRow({ todo }) {\n  return <li key={todo.id}>{todo.text}</li>;\n}\n\n// in the parent\n{todos.map(todo =>\n  <TodoRow todo={todo} />\n)}","takeaways":["The key goes on the element inside map(), not inside the extracted component."]},{"id":"reactint-003","courseId":"react-intermediate","domain":"components-jsx","topic":"Index as a key","prompt":"A to-do list renders rows with key={index}. Each row has its own uncontrolled note input. After the user deletes the first row, every note appears shifted onto the wrong task. Why?","hint":"Keys tell React which item is which between renders.","source":"https://react.dev/learn/rendering-lists","interaction":{"type":"single"},"options":[{"id":"a","text":"With index keys, row 1 now gets key 0, so React reuses the old first row's state and DOM for it","why":"Index keys follow position, not the item, so after a delete or reorder each surviving item inherits its neighbour's state. Use a stable id from the data."},{"id":"b","text":"Uncontrolled inputs never survive a list update, whatever key is used","whyNot":"With stable keys React moves each input with its item and keeps its value."},{"id":"c","text":"React needs a key on the input itself, not on the row","whyNot":"Keys belong on the elements returned from map; the input inside is matched by position within its row."},{"id":"d","text":"Deleting from an array in state is a mutation, so React cannot see the change","whyNot":"If the delete were a mutation nothing would re-render at all; here it re-renders, but with mismatched identity."}],"answer":"a","explanation":"A key gives each list item a stable identity. Index keys change meaning when items are inserted, deleted or reordered, so state follows the wrong item.","concept":"A key gives each list item a stable identity. Index keys change meaning when items are inserted, deleted or reordered, so state follows the wrong item.","rev":1,"kind":"scenario","takeaways":["Use ids from the data as keys; index keys are only safe for lists that never reorder or change."]},{"id":"reactint-004","courseId":"react-intermediate","domain":"components-jsx","topic":"Resetting state with a key","prompt":"A chat app renders <Chat contact={contact} />. When the user switches contacts, the half-typed draft from the previous contact stays in the box. What is the idiomatic fix?","hint":"Give React a reason to see each contact's chat as a different component.","source":"https://react.dev/learn/preserving-and-resetting-state","interaction":{"type":"single"},"options":[{"id":"a","text":"Add an effect in Chat that clears the draft whenever contact changes","whyNot":"It works but renders once with the stale draft first and adds an effect you don't need."},{"id":"b","text":"Store the draft in a ref so it is not kept between renders","whyNot":"A ref also persists for the life of the component, so the draft would still carry over."},{"id":"c","text":"Wrap Chat in memo so it re-creates its state when props change","whyNot":"memo only skips re-renders; it never resets state."},{"id":"d","text":"Render <Chat key={contact.id} contact={contact} /> so a new contact mounts a fresh Chat","why":"A different key at the same position makes React treat it as a different component, so it discards the old state and starts fresh."}],"answer":"d","explanation":"A key is part of a component's identity. Changing it resets all state in that subtree, which is the clean way to start over when the subject changes.","concept":"A key is part of a component's identity. Changing it resets all state in that subtree, which is the clean way to start over when the subject changes.","rev":1,"kind":"scenario","takeaways":["To reset a component's state when an id changes, give it key={id} instead of syncing with an effect."]},{"id":"reactint-005","courseId":"react-intermediate","domain":"components-jsx","topic":"A list of refs","prompt":"A carousel must scroll to any one of a changing number of images. A developer writes const ref = useRef(null) inside items.map(...) and the lint rule complains. What approach do the React docs describe?","hint":"Refs can hold any value, not only one DOM node.","source":"https://react.dev/learn/manipulating-the-dom-with-refs#how-to-manage-a-list-of-refs-using-a-ref-callback","interaction":{"type":"single"},"options":[{"id":"a","text":"Create the refs inside a useEffect after the list renders","whyNot":"Hooks cannot be called inside an Effect either; the problem is the same."},{"id":"b","text":"Keep one ref holding a Map, and give each item a ref callback that adds its node and returns a cleanup that removes it","why":"React calls the ref callback with the node and calls its returned cleanup when the ref is cleared, so the Map stays current."},{"id":"c","text":"Call useRef in the loop anyway, since the list length rarely changes","whyNot":"Hooks must be called at the top level; a loop breaks that rule whenever the length changes."},{"id":"d","text":"Give each image an id and call document.getElementById when scrolling","whyNot":"It bypasses React and is brittle; a ref callback gives you the nodes directly."}],"answer":"b","explanation":"You cannot call useRef per item, so pass a ref callback that stores each node in a Map held by one ref, and remove it in the cleanup.","concept":"You cannot call useRef per item, so pass a ref callback that stores each node in a Map held by one ref, and remove it in the cleanup.","rev":1,"kind":"scenario","takeaways":["For a ref per list item, use one ref holding a Map and a ref callback."]}]