Components and JSX
This is the component authoring surface — everything you actually type: JSX, props, children, event handlers, forms, and portals. Interviewers start here because it quickly shows whether you know what JSX compiles to and how data flows down a tree through composition rather than inheritance.
part ofReactoverview, primer and where to startread it →on this pageshowhide
explore
- Component Anatomy and Typing5 questions
- JSX Syntax and Semantics15 questions
- JSX Transform and Element Creation4 questions
- Expressions and Conditional Rendering4 questions
- List Rendering and Keys4 questions
- Fragments3 questions
- Props and Composition17 questions
- Children and Composition Patterns6 questions
- Render Props and Higher-Order Components6 questions
- Compound Components5 questions
- Event Handling14 questions
- The SyntheticEvent System5 questions
- Root Delegation and Native Interop4 questions
- Handler Identity and Inline Functions5 questions
- Forms and Inputs9 questions
- Controlled vs Uncontrolled Inputs5 questions
- Form State and Validation Patterns4 questions
- Portals4 questions
questions
64 · 6 sectionsIn React 19, what exactly is a function component, how does it differ from a class component, and is there anything a class can still do that a function cannot?
basics
~20 sA function component is a plain function that takes a props object and returns React elements, using hooks for state and effects. A class extends React.Component and implements render() plus lifecycle methods. Only error boundaries still require a class.
React requires components to be pure during render. What does purity mean for a React component, and which kinds of code violate it?
basics
~20 sA pure React component returns the same elements for the same props, state and context, and changes nothing outside itself while rendering: no mutating props or existing objects, no writing to module or global variables, no I/O. Side effects belong in event handlers or effects.
React 19 removed support for defaultProps on function components. How do you give a function component's props default values now, and what is left of defaultProps?
basics
~10 sUse JavaScript default parameter values in the destructuring of the props argument, such as function Button({ size = 'medium' }). React 19 ignores defaultProps on function components; it is still honoured on class components.
In a React + TypeScript codebase, how do you type a function component's props, and why do most teams avoid React.FC?
basics
~20 sType the props argument directly: function Button({ label }: ButtonProps). React.FC adds nothing you need, forces the return type, and since @types/react 18 no longer implies children, so teams declare children explicitly with React.ReactNode instead.
A reusable Button in a shared component library spreads every unrecognized prop onto the underlying <button> with {...rest}. What risks does that create, and how would you control it?
basics
~20 sBlind spreading leaks unknown props into the DOM, lets callers silently override the component's own attributes depending on spread order, and hides the real API from readers and types. Control it by destructuring what you own, spreading the rest first, and merging className and handlers deliberately.
In JSX, what does React render for `{value}` when value is a string, a number, true, null, undefined, an array, or a plain object?
basics
~20 sStrings and numbers render as text. Arrays render each element in order. Null, undefined, true and false render nothing at all. A plain object is not a valid child and React throws an error instead of rendering it.
In React, what is a Fragment, and what problem does returning `<>...</>` from a component solve that a wrapping `<div>` does not?
basics
~20 sA Fragment groups several children under one JSX parent while emitting no DOM node. A component can return multiple elements without an extra div that would distort CSS layout, break sibling and child selectors, invalidate HTML nesting, and add noise to the accessibility tree.
In React, how do you render an array of items in JSX, and why does React log the warning "Each child in a list should have a unique key prop"?
basics
~20 sReact renders a list by putting an array of elements in a JSX slot, normally items.map(item => <Row key={item.id} />). The key prop gives each element an identity independent of its position, so React can match it to the same item on the next render.
In JSX, why must a component tag be written `<Profile />` rather than `<profile />`, and what does React actually do if you lowercase it?
basics
~20 sCapitalization tells the JSX compiler whether the tag is a value or a string. A capitalized tag compiles to a reference to the variable of that name; a lowercase tag compiles to the string "profile", so React renders a literal DOM tag and your component never runs.
In a React component, `{cart.length && <CartSummary />}` puts a stray `0` on the page when the cart is empty. Why does that happen, and how do you fix it?
basics
~20 sJavaScript's && returns its left operand when that operand is falsy, so an empty cart makes the expression evaluate to the number 0. React skips null, undefined and booleans as children but renders numbers as text, so the 0 appears. Guard with cart.length > 0 instead.
In React, what does a component's `children` prop contain, how does JSX populate it, and what shapes can its value take at runtime?
basics
~20 schildren is an ordinary React prop that holds whatever JSX you nest between a component's opening and closing tags. Its value may be a string, a single element, an array of nodes, or undefined when nothing is nested.
Why did custom hooks replace render props and higher-order components as React's default way to reuse stateful logic? Name the concrete problems with the older patterns.
basics
~20 sBoth older patterns reuse logic by adding components: HOCs stack into deep wrapper chains with silent prop collisions and hidden prop origins, and render props nest into callback pyramids. A hook is a plain function call whose result you name yourself, adding nothing to the tree.
In React, what is the render-prop pattern — including the function-as-children form — and how does the callback get the data it renders?
basics
~20 sA render prop is a prop whose value is a function that returns JSX. The component holding the state calls that function during its own render and passes its data in as arguments, so the caller controls the markup while the component keeps the behaviour.
Inside a React component, why can calling `props.children.map(...)` throw or silently skip nodes, and what do the `Children` helpers exported by React do differently?
basics
~20 sThe children prop is not reliably an array: a single nested node is passed through as itself, so array methods are undefined on it. React's Children helpers normalize every shape, but they treat a fragment as one node and never look inside it.
React has no mechanism for one component to extend another the way a subclass extends a base class. Given a generic `Dialog` component, how do you build a `ConfirmDialog` that is always the confirm variant, and what is the general rule React expects you to follow?
basics
~20 sYou write ConfirmDialog as a normal component that renders Dialog with preset props and its own nested content. React reuses code by composition only: containment passes content through children, and specialization wraps a general component in a more specific one.
In React, an inline handler such as onClick={() => setOpen(true)} is a brand-new function on every render. Why does that happen, and when is it actually a problem?
basics
~20 sEvery render re-runs the component body, so the arrow expression evaluates again into a new function object. The allocation itself is cheap; the new identity only matters where something compares it, such as a memoized child or a dependency array.
In React JSX, what is the difference between writing onClick={handleClick} and onClick={handleClick()}, and what actually happens with the second form?
basics
~20 sThe first passes the function so React can call it on click. The second calls it during render and passes its return value, usually undefined, so nothing happens on click and the side effect fires on every render.
In a React onSubmit or onClick handler, why does `return false` not stop the browser's default behaviour, and what do you write instead?
basics
~10 sReact ignores a handler's return value entirely. Returning false only cancels defaults in inline HTML attributes and jQuery handlers. In React you call e.preventDefault(), and e.stopPropagation() separately if you also want to stop bubbling.
In React 19, what object does React pass to an event handler such as onClick, and how do you reach the underlying browser event from it?
basics
~10 sReact passes a SyntheticEvent: a cross-browser wrapper that mirrors the DOM event interface (type, target, preventDefault, stopPropagation). The untouched browser event is always available on e.nativeEvent for anything React does not normalize.
A React counter's click handler is created as useCallback(() => setCount(count + 1), []). The number goes to 1 and then stops changing. What is happening, and how do you fix it?
basics
~20 sThe empty dependency array pins the handler to the first render, where count was 0, so every click computes 0 + 1. Fix it with the updater form setCount(c => c + 1), or by listing count as a dependency.
In React, what makes a form input controlled versus uncontrolled, and what is the difference between the value and defaultValue props?
basics
~20 sA controlled input's displayed value comes from React state through the value prop and changes only when onChange updates that state. An uncontrolled input keeps its value inside the DOM node, seeded once by defaultValue, and you read it on demand.
In a React component, why does submitting a <form> reload the page, and what must the onSubmit handler do to stop that?
basics
~20 sThe browser's default form submission navigates, which reloads the page and throws away React state. Call event.preventDefault() as the first thing in the form's onSubmit handler, then read your state and send the request yourself.
React logs "A component is changing an uncontrolled input to be controlled". What causes that warning in a React form, and how do you fix it?
basics
~20 sThe input's value prop started as undefined or null, so React treated the field as uncontrolled, and a later render passed a string, flipping it to controlled. Fix it by initialising the state to an empty string or writing value={x ?? ''}.
Why can a React <input type="file"> not be a controlled component, and how do you read and clear the user's selection?
basics
~20 sBrowsers forbid script from setting which file is selected, so React cannot drive a file input from state — it is always uncontrolled. Read the selection from the node's files list or from FormData, and clear it by assigning an empty string to the node's value.
In a React form built from uncontrolled inputs, how do you read the field values when the user submits — with refs or with FormData?
basics
~20 sRead one or two fields through a ref, as inputRef.current.value. For a whole form, construct FormData from the form element in the submit handler and pull fields out by name, which needs no per-field wiring at all.
In React, what does createPortal(children, domNode) from react-dom do, and what layout problem does it solve for a modal or dropdown?
basics
~20 screatePortal renders children into any DOM element you name while keeping them in the same React tree. A modal can therefore escape an ancestor's overflow clipping and z-index stacking context without moving the component that owns its state.
A React component renders a modal with createPortal into document.body, yet clicks inside that modal still trigger an onClick handler on the component's own wrapper element. Why does the event reach a component that is not a DOM ancestor?
basics
~20 sReact propagates its events along the React element tree, not the document tree. Portal children are still React children of the component that rendered them, so onClick handlers on React ancestors run even though the DOM nodes live under document.body.
A dropdown closes on outside clicks using a document listener that ignores clicks where wrapperRef.current.contains(event.target). After the menu is moved into createPortal(menu, document.body), it now closes the instant you click an item inside it. What changed, and how do you fix it?
basics
~20 sNode.contains walks the DOM, and the portalled menu is no longer a DOM descendant of the wrapper, so inside clicks now look outside. Fix it by testing containment against both the trigger element and the portalled content, each with its own ref.
A design system's Modal, Tooltip, Select and Toast components all call createPortal(content, document.body). As the library owner, what would you standardize about the portal target, and what does portalling cost the consumers of those components?
basics
~20 sStandardize on one configurable portal root that lives inside the app's theming wrapper, injected through context so consumers can retarget it. The costs are lost CSS inheritance and scoping, global stacking order between overlays, reading order and focus that no longer match the visual layout, and no server-rendered markup.