skip to content

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 pageshow

explore

questions

64 · 6 sections

In 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?

level: juniorimportance: must knowfreq 80%
basics
~20 s

A 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.

open as a page

React requires components to be pure during render. What does purity mean for a React component, and which kinds of code violate it?

level: middleimportance: must knowfreq 62%
basics
~20 s

A 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.

open as a page

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?

level: middleimportance: should knowfreq 45%
basics
~10 s

Use 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.

open as a page

In a React + TypeScript codebase, how do you type a function component's props, and why do most teams avoid React.FC?

level: middleimportance: should knowfreq 55%
basics
~20 s

Type 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.

open as a page

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?

level: seniorimportance: should knowfreq 40%
basics
~20 s

Blind 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.

open as a page

In JSX, what does React render for `{value}` when value is a string, a number, true, null, undefined, an array, or a plain object?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Strings 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.

open as a page

In React, what is a Fragment, and what problem does returning `<>...</>` from a component solve that a wrapping `<div>` does not?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

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"?

level: juniorimportance: must knowfreq 85%
basics
~20 s

React 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.

open as a page

In JSX, why must a component tag be written `<Profile />` rather than `<profile />`, and what does React actually do if you lowercase it?

level: juniorimportance: must knowfreq 68%
basics
~20 s

Capitalization 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.

open as a page

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?

level: middleimportance: must knowfreq 78%
basics
~20 s

JavaScript'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.

open as a page

In React, what does a component's `children` prop contain, how does JSX populate it, and what shapes can its value take at runtime?

level: juniorimportance: must knowfreq 78%
basics
~20 s

children 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.

open as a page

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.

level: middleimportance: must knowfreq 60%
basics
~20 s

Both 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.

open as a page

In React, what is the render-prop pattern — including the function-as-children form — and how does the callback get the data it renders?

level: juniorimportance: should knowfreq 50%
basics
~20 s

A 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.

open as a page

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?

level: middleimportance: should knowfreq 36%
basics
~20 s

The 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.

open as a page

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?

level: middleimportance: should knowfreq 52%
basics
~20 s

You 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.

open as a page

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?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Every 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.

open as a page

In React JSX, what is the difference between writing onClick={handleClick} and onClick={handleClick()}, and what actually happens with the second form?

level: juniorimportance: must knowfreq 72%
basics
~20 s

The 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.

open as a page

In a React onSubmit or onClick handler, why does `return false` not stop the browser's default behaviour, and what do you write instead?

level: juniorimportance: must knowfreq 50%
basics
~10 s

React 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.

open as a page

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?

level: juniorimportance: must knowfreq 65%
basics
~10 s

React 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.

open as a page

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?

level: middleimportance: must knowfreq 64%
basics
~20 s

The 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.

open as a page

In React, what makes a form input controlled versus uncontrolled, and what is the difference between the value and defaultValue props?

level: juniorimportance: must knowfreq 85%
basics
~20 s

A 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.

open as a page

In a React component, why does submitting a <form> reload the page, and what must the onSubmit handler do to stop that?

level: juniorimportance: must knowfreq 70%
basics
~20 s

The 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.

open as a page

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?

level: middleimportance: must knowfreq 65%
basics
~20 s

The 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 ?? ''}.

open as a page

Why can a React <input type="file"> not be a controlled component, and how do you read and clear the user's selection?

level: middleimportance: should knowfreq 35%
basics
~20 s

Browsers 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.

open as a page

In a React form built from uncontrolled inputs, how do you read the field values when the user submits — with refs or with FormData?

level: middleimportance: should knowfreq 55%
basics
~20 s

Read 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.

open as a page

In React, what does createPortal(children, domNode) from react-dom do, and what layout problem does it solve for a modal or dropdown?

level: juniorimportance: should knowfreq 55%
basics
~20 s

createPortal 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.

open as a page

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?

level: middleimportance: should knowfreq 48%
basics
~20 s

React 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.

open as a page

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?

level: seniorimportance: should knowfreq 35%
basics
~20 s

Node.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.

open as a page

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?

level: principalimportance: nice to knowfreq 20%
basics
~20 s

Standardize 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.

open as a page