React Flashcards
What’s wrong with this JSX code?
ReactDOM.render(
<h1>Hello</h1>
<p>Welcome</p>
,
document.getElementById(“root”)
);
JSX must return a single element. Wrap <h1> and <p> in a parent element like a <div> or a Fragment.
In JSX, the HTML attribute ‘class’ becomes _________
className
Complete this component and export it:
function Welcome() {
return <h1>Welcome!</h1>;
}
// Export it so it can be used in other files
__________
export default Welcome;
Which of the following is not a valid way to render a React component named App?
A. <App></App>
B. ReactDOM.render(App, root)
C. ReactDOM.render(<App></App>, document.getElementById(“root”));
D. ReactDOM.render(<App></App>, root);
B — App needs to be wrapped in JSX (i.e., <App></App>), not passed as a raw function.
How do you access props inside a functional component?
As the first argument: function MyComponent(props) {…} or via destructuring: function MyComponent({ title }) {…}
When a component’s state updates via setState, what happens?
A. The page refreshes
B. The component re-renders
C. The component is unmounted
D. Nothing happens until you reload
B — React re-renders the component when state changes.
To declare a state variable in a functional component, use the hook: _________
useState
What is the initial value of count
here?
const [count, setCount] = useState(0);
0
Fix the error in this code:
function App() {
const [count, setCount] = useState
return <h1>{count}</h1>;
}
Missing parentheses — should be useState(0)
Which syntax would you use to conditionally render a component if a user is logged in?
{isLoggedIn ? <Dashboard></Dashboard> : <Login></Login>}
What will this code render?
{isLoggedIn && <p>Welcome back!</p>}
If isLoggedIn is true, it renders <p>Welcome back!</p>. If false, it renders nothing.
In React, what’s a common pattern to avoid ‘else’ blocks in JSX?
Use short-circuit evaluation with && for simple conditions.
What happens if you forget to call event.preventDefault() in a form submission handler?
The page will reload (default browser behavior).
Why do we set the value of an <input></input> element to a state variable in React?
To create a controlled component — React becomes the single source of truth for the input value.
Fill in the blank to handle input changes in a controlled form:
function handleChange(event) {
_________
}
setValue(event.target.value);
Which event prop do you use to handle a button click in React?
onClick
How do you pass a function with an argument to an onClick handler without calling it immediately?
Use an arrow function: onClick={() => myFunction(arg)}
How do you stop a synthetic event from refreshing the page?
Call event.preventDefault() in the handler.
What React feature allows you to access input changes without needing to use class components?
useState (a React hook)
What naming convention is used for React component files and functions?
PascalCase (e.g., MyComponent.jsx, function MyComponent() {})
How do you pass data to a component from its parent?
Via props: <ChildComponent></ChildComponent>
How do you access props inside a functional component using destructuring?
function MyComponent({ title }) { return <h1>{title}</h1>; }
How do you set a default value for a prop?
Use ComponentName.defaultProps = { title: ‘Default’ }
What does React require each child in a list to have?
A unique ‘key’ prop