[{"id":"reactadv-001","courseId":"react-advanced","domain":"components-jsx","topic":"Custom elements: properties vs attributes","prompt":"A design system ships a web component, and the app renders it as below. The element's class sets this.series = undefined in its constructor. What does the element receive from React?","hint":"React looks at the element's class before choosing how to hand over a value.","source":"https://react.dev/reference/react-dom/components#attributes-vs-properties","interaction":{"type":"single"},"options":[{"id":"a","text":"The string \"3,5,8\" in a series attribute, because React always passes values to custom elements as attributes","whyNot":"That is only the fallback; a property defined on the class during construction gets the raw value."},{"id":"b","text":"The array itself, set as the series property, because that property exists on the class when it is constructed","why":"React passes arbitrary values as properties when the name shows up on the class during construction; otherwise it serializes them into attributes."},{"id":"c","text":"Nothing, because React drops non-string values on any tag whose name contains a dash","whyNot":"React does not drop them; without a matching property it serializes them, here to \"3,5,8\"."},{"id":"d","text":"A JSON string in a data-series attribute that the element has to parse itself","whyNot":"React never invents data- attributes or JSON; it uses a property or a toString attribute."}],"answer":"b","explanation":"For a tag with a dash, React sets a property when the element's class defines it during construction, and otherwise serializes the value into a string attribute.","concept":"For a tag with a dash, React sets a property when the element's class defines it during construction, and otherwise serializes the value into a string attribute.","rev":1,"kind":"scenario","code":"<chart-card series={[3, 5, 8]} />","takeaways":["To get objects into a custom element, define the property on its class in the constructor."]},{"id":"reactadv-002","courseId":"react-advanced","domain":"components-jsx","topic":"Ancestor type change resets subtree","prompt":"A layout toggle swaps the wrapper of a stateful Counter between a section and a div, keeping Counter as the only child. Users report the count resets on every toggle. Why?","hint":"React compares the tree from the top down, not just the component you care about.","source":"https://react.dev/learn/preserving-and-resetting-state#different-components-at-the-same-position-reset-state","interaction":{"type":"single"},"options":[{"id":"a","text":"Counter appears in two branches of a ternary, and React always resets state across branches","whyNot":"Branches don't matter; two Counters at the same position in the returned tree keep their state."},{"id":"b","text":"Counter receives different props on each toggle, and changed props reset a component's state","whyNot":"Changing props never resets state; it only re-renders the component."},{"id":"c","text":"React keys each element by its line in the source, and the two Counter tags sit on different lines","whyNot":"React knows nothing about source positions; it compares the rendered tree."},{"id":"d","text":"The element type at the parent position changed, so React removes the old subtree, Counter and its state included","why":"When a div replaces a section, everything under it is torn down. State survives only if the tree structure matches between renders."}],"answer":"d","explanation":"Changing the type of an ancestor element destroys the whole subtree below it, so descendants lose their state even if they look identical.","concept":"Changing the type of an ancestor element destroys the whole subtree below it, so descendants lose their state even if they look identical.","rev":1,"kind":"scenario","code":"return compact ? (\n  <div><Counter /></div>\n) : (\n  <section><Counter /></section>\n);","takeaways":["State survives only when the tree structure matches from the root down to the component."]},{"id":"reactadv-003","courseId":"react-advanced","domain":"components-jsx","topic":"Stylesheets with precedence","prompt":"On React 19, a MapPanel deep in the tree renders the link below, and two other widgets render the same link. MapPanel appears only after a user action. What does React do with these links?","hint":"With a precedence, React treats the stylesheet as a resource it manages, not a plain element.","source":"https://react.dev/reference/react-dom/components/link#linking-to-a-stylesheet","interaction":{"type":"single"},"options":[{"id":"a","text":"It puts one link in the head, ordered by precedence, and MapPanel suspends while the sheet is still loading","why":"React hoists the link to the head, de-duplicates links with the same href, ranks it by precedence, and the rendering component suspends while it loads."},{"id":"b","text":"It renders three link tags where the components sit in the body, in render order","whyNot":"With precedence, React places the element in the head and keeps a single link per href."},{"id":"c","text":"It puts one link in the head, but MapPanel paints unstyled first and restyles on load","whyNot":"The component that renders the link suspends while the sheet loads, so it does not paint unstyled."},{"id":"d","text":"It ignores the links, because stylesheets must be added through a framework's head API","whyNot":"React 19 supports rendering stylesheet links from any component."}],"answer":"a","explanation":"A link rel=\"stylesheet\" with a precedence prop is hoisted to the head, de-duplicated by href, ordered by precedence, and suspends the component that renders it until it loads.","concept":"A link rel=\"stylesheet\" with a precedence prop is hoisted to the head, de-duplicated by href, ordered by precedence, and suspends the component that renders it until it loads.","rev":1,"kind":"scenario","code":"<link\n  rel=\"stylesheet\"\n  href=\"/map.css\"\n  precedence=\"medium\"\n/>","takeaways":["Give component stylesheets a precedence: React hoists, de-duplicates and waits for them."]},{"id":"reactadv-004","courseId":"react-advanced","domain":"components-jsx","topic":"Mutation after JSX creation","prompt":"A staff engineer flags this component even though it renders correctly in today's tests. What is the strongest argument against it?","hint":"Ask when React is allowed to read a value once it has gone into an element.","source":"https://react.dev/reference/rules/components-and-hooks-must-be-pure#values-are-immutable-after-being-passed-to-jsx","interaction":{"type":"single"},"options":[{"id":"a","text":"JSX elements must be created inside the return statement, never stored in variables first","whyNot":"Storing elements in variables is normal; the problem is what happens to styles afterwards."},{"id":"b","text":"Mutating any object during render is a side effect, so even the local styles object breaks purity","whyNot":"Mutating a value created in the same render is allowed local mutation; the issue is mutating it after it went into JSX."},{"id":"c","text":"React may evaluate JSX eagerly, so mutating a value after it was used in JSX can leave Header with outdated styles","why":"Once a value is passed to JSX it should be treated as immutable. Create a separate object for the footer instead of changing the shared one."},{"id":"d","text":"Two elements sharing one props object makes React merge them into a single node during commit","whyNot":"React does not merge elements by shared props; they stay separate elements."}],"answer":"c","explanation":"Values become immutable once passed to JSX, because React may read them eagerly before the component finishes rendering.","concept":"Values become immutable once passed to JSX, because React may read them eagerly before the component finishes rendering.","rev":1,"kind":"scenario","code":"function Page({ colour }) {\n  const styles = { colour, size: 'large' };\n  const header = <Header styles={styles} />;\n  styles.size = 'small';\n  const footer = <Footer styles={styles} />;\n  return <>{header}{footer}</>;\n}","takeaways":["Do every mutation before creating the JSX, or build a new value for the next element."]},{"id":"reactadv-005","courseId":"react-advanced","domain":"components-jsx","topic":"suppressHydrationWarning","prompt":"A server-rendered footer shows the time it was rendered, as below, and hydration logs a mismatch. A teammate proposes suppressHydrationWarning. Which TWO statements match the React docs?","hint":"Think of it as a narrow escape hatch, not a switch for the whole page.","source":"https://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"It goes on the element whose own text or attribute differs, such as this p, and only works one level deep","why":"The docs say it silences the warning for that element only, one level deep."},{"id":"b","text":"Set it once on the root layout to silence mismatches anywhere below it","whyNot":"It only works one level deep, so it cannot cover a whole subtree."},{"id":"c","text":"It is meant for unavoidable differences such as timestamps, not as a fix for bugs in render logic","why":"The docs call it an escape hatch for unavoidable mismatches like a timestamp and warn against overusing it."},{"id":"d","text":"With it set, React patches the text to the client's value while hydrating","whyNot":"React will not attempt to patch mismatched text content; it only stops warning."}],"answer":"ac","explanation":"suppressHydrationWarning silences an unavoidable mismatch on one element, one level deep. It does not patch the text and is not a general fix.","concept":"suppressHydrationWarning silences an unavoidable mismatch on one element, one level deep. It does not patch the text and is not a general fix.","rev":1,"kind":"scenario","code":"const time =\n  new Date().toLocaleTimeString();\nreturn <p>Rendered at {time}</p>;","takeaways":["suppressHydrationWarning: one element, one level deep, for things like timestamps."]}]