What is JSX?
A syntax extension for JavaScript that lets you write HTML-like markup inside JS files. It’s stricter than HTML — you must close all tags (e.g. <br></br>) and a component can only return one root element (wrap with <> … </> if needed).
What’s the rule for React component names vs HTML tags?
React component names must start with a capital letter (e.g. <MyButton></MyButton>). HTML tags must be lowercase (e.g. <div>). This is how React distinguishes them.
How do you embed JavaScript expressions in JSX?
Use curly braces {}. For content: {user.name}. For attributes: src={user.imageUrl}. For inline styles: style={{ color: ‘red’ }} (double curlies = an object inside JSX curlies).
How do you specify a CSS class in JSX?
Use className instead of class: <img></img>. This is because ‘class’ is a reserved word in JavaScript.
What does ‘export default’ do in a component file?
It specifies the main component in the file. Other components can be exported with named exports. A file can have one default export but many named exports.
What are the two types of ‘model’ data in React?
Props and State. Props are like function arguments — passed from parent to child. State is a component’s memory — it lets a component track info and change it in response to interactions.
What does ‘lifting state up’ mean?
Moving state from child components to their closest common parent, then passing it back down as props. This lets sibling components share and stay in sync with the same data.
What is conditional rendering in React?
Using standard JS techniques (if/else, ternary ? :, logical &&) to conditionally include JSX. There’s no special React syntax for conditions.
Why do list items need a ‘key’ prop?
Keys let React identify which items changed, were added, or removed. They should be unique among siblings (typically a database ID). Never use array index as key if the list can reorder.
What is a React Fragment (<> </>)?
An empty wrapper that lets you return multiple JSX elements without adding extra DOM nodes. Equivalent to <React.Fragment>. Use <Fragment key={id}> when you need a key.</React.Fragment>
Why must components be pure functions?
A pure component only calculates its output — given the same inputs (props, state, context), it always returns the same JSX. It should not mutate pre-existing variables or objects. This lets React optimize rendering, skip redundant work, and enable features like Suspense.
What is React’s component tree?
A hierarchical structure where the root component renders child components, which render their own children. React uses this tree to determine render order and manage state. State is tied to a component’s position in this tree.
What are Hooks in React?
Functions starting with ‘use’ that let you tap into React features (state, effects, context, etc.) from function components. Built-in hooks include useState, useEffect, useRef, useContext, useReducer, useMemo, useCallback, and more.
What are the Rules of Hooks?
1) Only call hooks at the TOP LEVEL of your component or custom hook — never inside loops, conditions, or nested functions. 2) Only call hooks from React function components or custom hooks. This ensures React can correctly track hook state across renders.
Why can’t you call hooks inside conditions or loops?
React relies on the ORDER hooks are called to match each hook with its state. If you conditionally skip a hook, every hook after it gets the wrong state. Extract a new component instead if you need conditional hook usage.
What does useState return?
A pair: [currentValue, setterFunction]. Convention: const [thing, setThing] = useState(initialValue). The setter triggers a re-render with the new value.
Does useState merge objects like class setState?
No. Unlike class components, the setter REPLACES the entire value. For objects, spread the previous state: setUser({ …user, name: ‘New’ }). Same for arrays — never mutate directly.
What is a functional update with useState?
Passing a function to the setter: setCount(prev => prev + 1). Use this when the new state depends on the previous state, especially when batching multiple updates. React will pass the latest pending state as ‘prev’.
What does ‘state as a snapshot’ mean?
When you call setState, the current render’s state variable doesn’t change. The new value is available on the NEXT render. Each render has its own snapshot of state, props, and event handlers.
What happens if you call setState 3 times with the same value in one handler?
React batches state updates. If you do setCount(count + 1) three times, count is the same stale value each time — you only get +1, not +3. Use functional updates: setCount(c => c + 1) three times to get +3.
How do you update objects in state immutably?
Create a new object with spread: setUser({ …user, name: ‘New’ }). For nested objects: setUser({ …user, address: { …user.address, city: ‘NYC’ } }). Never mutate state objects directly — React won’t detect the change.
How do you update arrays in state immutably?
Use non-mutating methods: filter() to remove, map() to transform, […arr, newItem] to add, .slice() to copy a portion. Avoid push, pop, splice, reverse, sort directly on state arrays — copy first.
What is useEffect for?
Synchronizing your component with an external system (APIs, browser DOM, timers, subscriptions, etc.). It runs AFTER the component renders and paints to the screen. It’s an ‘escape hatch’ from React’s declarative model.
What do the different dependency arrays mean in useEffect?
useEffect(fn) — runs after EVERY render. useEffect(fn, []) — runs only after FIRST render (mount). useEffect(fn, [a, b]) — runs after renders where a or b changed.