[{"id":"reactbeg-001","courseId":"react-beginner","domain":"components-jsx","topic":"Component names","prompt":"A graduate writes a component and uses it, but the page shows nothing where the profile should be and the console hints at an unknown tag. What is the fix?","hint":"Think about how React tells your own components apart from HTML tags.","source":"https://react.dev/learn/your-first-component","interaction":{"type":"single"},"options":[{"id":"a","text":"Wrap profile in a Fragment so it returns a single root","whyNot":"It already returns a single img element, so a Fragment changes nothing."},{"id":"b","text":"Rename it Profile and render it as <Profile />","why":"React treats lowercase tags as HTML elements and capitalised ones as components. A component name must start with a capital letter."},{"id":"c","text":"Export profile as a named export instead of a local function","whyNot":"How a function is exported has nothing to do with how JSX resolves a lowercase tag."},{"id":"d","text":"Call it as {profile()} instead of using a JSX tag","whyNot":"Calling a component as a plain function bypasses React; the real fix is the capital letter."}],"answer":"b","explanation":"JSX uses casing to decide: lowercase tags are built-in HTML elements, capitalised tags are your components.","concept":"JSX uses casing to decide: lowercase tags are built-in HTML elements, capitalised tags are your components.","rev":1,"kind":"scenario","code":"function profile() {\n  return <img src=\"/me.png\" alt=\"Me\" />;\n}\n\nexport default function Gallery() {\n  return <section><profile /></section>;\n}","takeaways":["Component names start with a capital letter, or React treats them as HTML tags."]},{"id":"reactbeg-002","courseId":"react-beginner","domain":"components-jsx","topic":"Nested component definitions","prompt":"In this form, the text field loses what you typed every time the outer counter changes. An interviewer asks why. What is the best answer?","hint":"Ask what TextField is, as a value, on each render of Form.","source":"https://react.dev/learn/your-first-component","interaction":{"type":"single"},"options":[{"id":"a","text":"The input has no key, so React cannot match it between renders","whyNot":"Keys matter for lists of siblings; a single child keeps its identity without one."},{"id":"b","text":"Clicking the button runs a batched update that clears all child state","whyNot":"Batching groups updates; it never clears another component's state."},{"id":"c","text":"TextField is a new function on each render, so React sees a new component and resets it","why":"A component defined inside another is recreated on every render, so React treats it as a different type at that position and drops its state. Declare components at the top level."},{"id":"d","text":"useState inside TextField breaks the Rules of Hooks and loses state","whyNot":"Calling useState at the top of TextField follows the rules; the problem is where TextField itself is declared."}],"answer":"c","explanation":"Never define a component inside another component: each render creates a new type, so React remounts it and its state resets.","concept":"Never define a component inside another component: each render creates a new type, so React remounts it and its state resets.","rev":1,"kind":"scenario","code":"function Form() {\n  const [count, setCount] = useState(0);\n  function TextField() {\n    const [text, setText] = useState('');\n    return <input value={text}\n      onChange={e => setText(\n        e.target.value)} />;\n  }\n  const inc = () => setCount(count + 1);\n  return (<>\n    <TextField />\n    <button onClick={inc}>{count}</button>\n  </>);\n}","takeaways":["Declare every component at the top level of the module and pass data in with props."]},{"id":"reactbeg-003","courseId":"react-beginner","domain":"components-jsx","topic":"Single root and Fragments","prompt":"A component returns an h1 and a ul side by side and fails to compile. The team does not want an extra div in the HTML. What should they do?","hint":"There is a way to group elements that leaves nothing behind in the DOM.","source":"https://react.dev/learn/writing-markup-with-jsx","interaction":{"type":"single"},"options":[{"id":"a","text":"Wrap both in <>...</>, which groups them without adding a DOM node","why":"JSX compiles to objects and a function can return only one, so you need a single parent; a Fragment groups them without leaving any trace in the HTML."},{"id":"b","text":"Return them separated by a comma inside the parentheses","whyNot":"The comma operator returns only the last value, so the h1 would be lost."},{"id":"c","text":"Move the ul into a second return statement","whyNot":"A function stops at the first return; the second one never runs."},{"id":"d","text":"Wrap them in a <span> because it is inline and invisible","whyNot":"A span is still a real DOM node, which is exactly what the team wanted to avoid."}],"answer":"a","explanation":"A component returns one root because JSX becomes a single JavaScript object; a Fragment satisfies that without adding markup.","concept":"A component returns one root because JSX becomes a single JavaScript object; a Fragment satisfies that without adding markup.","rev":1,"kind":"scenario","code":"function Todos() {\n  return (\n    <h1>Todos</h1>\n    <ul>...</ul>\n  );\n}","takeaways":["Use <>...</> to return several elements without an extra wrapper in the DOM."]},{"id":"reactbeg-004","courseId":"react-beginner","domain":"components-jsx","topic":"JSX attribute names","prompt":"A developer is porting an HTML snippet to JSX. Which TWO attributes are written correctly for JSX?","hint":"JSX attributes become JavaScript object keys, with one historical exception.","source":"https://react.dev/learn/writing-markup-with-jsx","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"class=\"card\"","whyNot":"class is a reserved word in JavaScript, so JSX uses className."},{"id":"b","text":"className=\"card\"","why":"JSX uses className, named after the DOM property, because class is reserved."},{"id":"c","text":"aria-label=\"Close\"","why":"For historical reasons, aria-* and data-* attributes keep their dashes in JSX."},{"id":"d","text":"stroke-width=\"2\"","whyNot":"Most dashed SVG and HTML attributes become camelCase in JSX, so this is strokeWidth."}],"answer":"bc","explanation":"Most attributes are camelCase in JSX because they become object keys, but aria-* and data-* keep their HTML dashes.","concept":"Most attributes are camelCase in JSX because they become object keys, but aria-* and data-* keep their HTML dashes.","rev":1,"kind":"scenario","takeaways":["className and camelCase attributes in JSX; aria-* and data-* stay dashed."]},{"id":"reactbeg-005","courseId":"react-beginner","domain":"components-jsx","topic":"Inline styles in JSX","prompt":"A junior writes style=\"background-color: black\" on a ul in JSX and it does not work. How should the inline style be written?","hint":"In JSX, style takes a JavaScript value, not a CSS string.","source":"https://react.dev/learn/javascript-in-jsx-with-curly-braces","interaction":{"type":"single"},"options":[{"id":"a","text":"style={'background-color: black'}","whyNot":"React expects an object for style, not a CSS string."},{"id":"b","text":"style={{ 'background-color': 'black' }}","whyNot":"It is an object, but inline style keys are written in camelCase in React."},{"id":"c","text":"style={{ backgroundColor: 'black' }}","why":"The outer braces open a JavaScript expression and the inner ones are an object literal; inline style keys are camelCase."},{"id":"d","text":"style={[backgroundColor, 'black']}","whyNot":"An array is not how React describes styles; it needs an object of property names."}],"answer":"c","explanation":"The double braces in style={{ }} are just an object inside JSX braces, and its keys use camelCase.","concept":"The double braces in style={{ }} are just an object inside JSX braces, and its keys use camelCase.","rev":1,"kind":"scenario","takeaways":["{{ }} is not special syntax: it is an object inside JSX curly braces."]}]