Quiz · Medium
Twenty-two questions on JSX rules, props and one-way data flow, state snapshots and immutable updates, keys, conditional rendering and the render cycle, with diagram questions.
What does JSX compile to?
CSS-in-JS objects
HTML strings that are inserted with innerHTML
A separate template language that React parses at runtime
Plain JavaScript function calls that create React elements
A compiler (Babel, SWC, TypeScript) turns JSX into calls like jsx('div', { children: ... }). These return plain objects (elements) that describe the UI; React turns them into DOM updates. It is neither a string template nor HTML.
Which JSX is correct for adding a CSS class and an inline red color to a div?
<div className="box" style={color: "red"}>
<div class="box" style="color: red">
<div class=box style={{ color: red }}>
<div className="box" style={{ color: "red" }}>
JSX uses className, and style takes a JavaScript object, hence the double braces (outer braces for the JS expression, inner braces for the object literal). A style string and class are HTML, and {color: "red"} with one pair of braces is a syntax error.
Why must a custom component be named <Card /> and not <card />?
React only supports PascalCase for performance
Lowercase tags are treated as built-in DOM elements; capitalized names are looked up as components
Lowercase names are reserved for class components
Lowercase tags are removed by the compiler
JSX decides what to render by the first letter: div becomes the string 'div' (a DOM element), Card becomes a reference to your function or class named Card.
In the diagram, how does the AddTodoForm component make the App component add a todo?
It calls a function passed down as a prop (onAdd), and App updates its own state
It sends the data up through context only
It re-renders App by calling App() itself
It directly modifies App's state
React has one-way data flow: parents pass data down as props, children never write to the parent's state. To ask for a change, the parent passes a callback prop, the child calls it, and the parent calls its own setState, which re-renders the tree.
A component declares const [count, setCount] = useState(0). A click handler runs setCount(count + 1) three times in a row. What is count after the re-render?
It throws an error
0
1
3
Each call uses the same snapshot value count = 0, so all three queue setCount(1). To increase by 3, use updater functions: setCount(c => c + 1) three times.
Which statement correctly adds an item to an array held in state?
setItems([...items, newItem])
items[items.length] = newItem; setItems(items)
items.push(newItem)
setItems(items.push(newItem))
State must be treated as immutable: create a new array. push mutates the existing array (and returns a number, so the last option would set state to a number). Calling setItems(items) with the same reference does not re-render.
A component renders {items.length && <List items={items} />} with items = []. What appears on the screen?
The text "false"
The number 0
An empty list
Nothing
0 && x evaluates to 0, and React renders numbers (0 included). Only null, undefined, true and false render nothing. Use items.length > 0 && ... or a ternary.
What is wrong with <button onClick={deleteItem(id)}>Delete</button>?
onClick must be lowercase
Nothing, this is the standard way
deleteItem(id) runs during render; it should be wrapped: onClick={() => deleteItem(id)}
id cannot be passed to event handlers
The expression inside the braces is evaluated while rendering, so the function is called immediately and its return value (often undefined) becomes the handler. Pass a function, not a function call.
What is the best key for items in a list of todos loaded from a database?
Math.random()
The unique id of each todo
The todo text
The array index
Keys must be stable and unique among siblings. Ids keep identity when the list is reordered or filtered. Indexes break state when items move, random keys change on every render (remounting everything), and text can repeat or be edited.
Looking at this diagram, what does React do after comparing the old and new virtual DOM trees?

It updates all three li elements to be safe
It reloads the page
It updates only the changed part of the real DOM (removes the class attribute on the first li)
It replaces the entire ul in the real DOM
Reconciliation finds the minimal difference (here the className prop went from 'selected' to null) and applies just that change in the commit phase.
Image: Moderateasneeded, CC BY-SA 4.0, via Wikimedia Commons.
In the render cycle diagram, which step may update the real DOM?
Cleanup
Commit
Trigger
Render
Rendering only calls your components to compute the next UI (it must be pure). In the commit phase React applies the changes to the DOM. Effects then run after the browser paints.
Which of these makes a component re-render?
A ref.current value changes
console.log is called inside it
Its state changes
A local let variable inside it changes
State updates (and parent re-renders or context changes) trigger renders. Local variables are recreated each render and refs are mutable boxes that React does not watch.
What is a controlled input?
An input with the disabled attribute
An input that React validates automatically
An input whose value comes from React state, updated via onChange
An input rendered only by the server
<input value={text} onChange={e => setText(e.target.value)} />: React state is the source of truth. An uncontrolled input keeps its own value in the DOM.
Which one is not allowed by the Rules of Hooks?
Calling useState at the top of a function component
Calling useEffect at the top of a custom hook
Calling a custom hook at the top level of a component
Calling useState inside an if block
Hooks must run in the same order on every render, so they cannot be put in conditions, loops or nested functions. You can put the condition inside the hook's callback instead.
True or False: a component may change its own props (for example props.title = 'New').
False
True
Props are read-only. A component should treat them as immutable inputs; if something needs to change, it belongs in state, or the parent must pass new props.
What is the purpose of the children prop?
It holds the state of child components
It is the array of keys of rendered elements
It lists the component's child functions
It contains whatever JSX is nested between a component's opening and closing tags
For <Card><p>Hi</p></Card>, Card receives props.children equal to the <p> element. This makes wrappers and layouts easy.
What does <>...</> do in JSX?
Marks a component as a client component
Groups children into one parent without adding an extra DOM element
Comments out the code between
Creates a portal
It is shorthand for <React.Fragment>. Returning a Fragment lets a component return several siblings without an extra wrapper div.
Why do two sibling components that both need the same changing value usually get that state from their parent?
State is shared automatically between siblings
Sibling components cannot render at the same time
Lifting state up keeps one source of truth that both read through props
React forbids components from having state
If each sibling kept its own copy, they would get out of sync. The common parent owns the state and passes it down (plus setters or callbacks).
An <input> inside a UserForm keeps stale text when you switch from user A to user B. Which fix is idiomatic?
Wrap the form in React.memo
Use Math.random() as a key on every render
<UserForm key={user.id} user={user} />
Call window.location.reload()
A different key makes React unmount the old instance and mount a fresh one, resetting its state. A random key would reset on every render, and reloading the page throws away everything.
Which behavior does React.StrictMode have in development?
It disables hooks inside loops at runtime
It renders components (and runs effects) an extra time to expose impure code and missing cleanups
It blocks network requests made during render
It makes the production build smaller
StrictMode is a development-only checker. It double-invokes render functions and, for effects, runs mount, unmount, mount again, so bugs from impure render or missing cleanup show up early. It does not change production.
What is the correct way to update one field of an object in state?
setUser(user.name = 'Ana')
setUser({ ...user, name: 'Ana' })
setUser(user) after changing user.name
user.name = 'Ana'
Create a new object that copies the old fields and overrides one (spread, then the changed key). Mutating and re-setting the same object reference does not reliably trigger a re-render.
What does createPortal(<Modal />, document.body) do?
Creates a new React root with separate state
Renders the modal on the server
Copies the modal to every page
Renders the modal into document.body while it stays in the same place in the React tree
Portals render children into a different DOM node (useful for modals and tooltips that must escape overflow or z-index) but context and event bubbling still follow the React tree.