React Flashcards

1
Q

What’s wrong with this JSX code?

ReactDOM.render(

<h1>Hello</h1>

<p>Welcome</p>

,
document.getElementById(“root”)
);

A

JSX must return a single element. Wrap <h1> and <p> in a parent element like a <div> or a Fragment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In JSX, the HTML attribute ‘class’ becomes _________

A

className

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Complete this component and export it:

function Welcome() {
return <h1>Welcome!</h1>;
}

// Export it so it can be used in other files
__________

A

export default Welcome;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);

A

B — App needs to be wrapped in JSX (i.e., <App></App>), not passed as a raw function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you access props inside a functional component?

A

As the first argument: function MyComponent(props) {…} or via destructuring: function MyComponent({ title }) {…}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

B — React re-renders the component when state changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To declare a state variable in a functional component, use the hook: _________

A

useState

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the initial value of count here?

const [count, setCount] = useState(0);

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fix the error in this code:

function App() {
const [count, setCount] = useState
return <h1>{count}</h1>;
}

A

Missing parentheses — should be useState(0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which syntax would you use to conditionally render a component if a user is logged in?

A

{isLoggedIn ? <Dashboard></Dashboard> : <Login></Login>}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will this code render?

{isLoggedIn && <p>Welcome back!</p>}

A

If isLoggedIn is true, it renders <p>Welcome back!</p>. If false, it renders nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In React, what’s a common pattern to avoid ‘else’ blocks in JSX?

A

Use short-circuit evaluation with && for simple conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens if you forget to call event.preventDefault() in a form submission handler?

A

The page will reload (default browser behavior).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why do we set the value of an <input></input> element to a state variable in React?

A

To create a controlled component — React becomes the single source of truth for the input value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Fill in the blank to handle input changes in a controlled form:

function handleChange(event) {
_________
}

A

setValue(event.target.value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which event prop do you use to handle a button click in React?

A

onClick

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you pass a function with an argument to an onClick handler without calling it immediately?

A

Use an arrow function: onClick={() => myFunction(arg)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you stop a synthetic event from refreshing the page?

A

Call event.preventDefault() in the handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What React feature allows you to access input changes without needing to use class components?

A

useState (a React hook)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What naming convention is used for React component files and functions?

A

PascalCase (e.g., MyComponent.jsx, function MyComponent() {})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you pass data to a component from its parent?

A

Via props: <ChildComponent></ChildComponent>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you access props inside a functional component using destructuring?

A

function MyComponent({ title }) { return <h1>{title}</h1>; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you set a default value for a prop?

A

Use ComponentName.defaultProps = { title: ‘Default’ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does React require each child in a list to have?

A

A unique ‘key’ prop

25
What happens if a component is rendered without a required prop?
It may still render, but could result in undefined behavior unless propTypes are used.
26
Fill in the blank to define propTypes for a component: MyComponent.propTypes = { title: __________, };
PropTypes.string.isRequired
27
What is the benefit of using propTypes in a component?
It helps validate the type of props passed, improving maintainability and debugging.
28
How do you pass a callback function to a child component?
As a prop:
29
What’s the equivalent of emitting an event in Vue when using React?
Call a function passed via props from the child component.
30
In TypeScript, how do you define a click event type in React?
Use: (event: React.MouseEvent) => void
31
How do you define a component prop type using an interface?
interface Props { title: string }
32
How do you annotate a component that receives props with TypeScript?
const Component: React.FC = ({ title }) => { ... }
33
What TypeScript type should you use for a function prop that takes a string and returns nothing?
(value: string) => void
34
What's the type for the 'children' prop in a TypeScript component?
ReactNode (from 'react')
35
How do you define a union type for props in TypeScript?
type ButtonProps = 'primary' | 'secondary';
36
How do you extend an existing interface in TypeScript?
interface NewProps extends ExistingProps { ... }
37
What's a key difference between 'type' and 'interface' in TypeScript?
Interfaces can be reopened and extended; types are more flexible with unions/intersections.
38
What is the correct syntax to import a type in TypeScript?
import type { MyType } from './types';
39
Why is typing props useful in a React app using TypeScript?
Provides better autocompletion, catches errors at compile time, and improves maintainability.
40
Which hook allows you to use state in a functional component?
useState
41
Which hook is used to perform side effects in a functional component?
useEffect
42
What is the equivalent of componentDidMount in useEffect?
useEffect(() => { ... }, [])
43
How do you clean up in useEffect (like componentWillUnmount)?
Return a function from useEffect: useEffect(() => { return () => { ... } }, [])
44
What does useEffect run by default when no dependency array is provided?
After every render
45
How do you trigger useEffect only when a specific value changes?
Include it in the dependency array: useEffect(() => { ... }, [value])
46
What happens if you update state directly in useEffect without dependencies?
It can cause an infinite render loop.
47
How do you update a state variable based on the previous value?
Use a callback: setCount(prev => prev + 1)
48
Can you call hooks conditionally inside components?
No — hooks must be called unconditionally in the top-level of the component.
49
Why must hooks be called at the top level of a component?
To ensure React can track hook order between renders.
50
Which array method is commonly used in React to render lists of components?
map()
51
What must each element rendered with map() have?
A unique 'key' prop
52
How would you remove an item from a list in state based on its index?
Use filter: array.filter((_, i) => i !== index)
53
Which array method would you use to sum up values?
reduce()
54
Fill in the blank to render a list of names: {names.________(name =>
  • {name}
  • )}
    map
    55
    How can you conditionally render only active users in a list?
    Use filter before map: users.filter(u => u.active).map(...)
    56
    What does the map method return?
    A new array with the results of calling a function on every element.
    57
    True or False: map() modifies the original array.
    False — it returns a new array and leaves the original unchanged.
    58
    What’s a common mistake when using map to render components in React?
    Forgetting to add a key prop or using an unstable key like array index.
    59
    Why is using index as a key in map discouraged?
    It can cause rendering issues when items are reordered or removed.