React Basics Flashcards

Basic React Concepts

1
Q

What is the primary reason for preferring functional components over class components in React?

A

Since the introduction of hooks (16.8), functional components can do everything class components can do with simpler syntax and better performance characteristics.

  1. Easier to test
  2. Less boilerplate
  3. more declarative
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What principle do you follow when breaking down a complex UI into components?

A

I follow the single responsibility principle.

  1. Atomic components (buttons, inputs)
  2. Molecules (form groups, card components)
  3. Organisms (entire sections like nav bars)
  4. Templates (page layouts)
  5. Pages (complete views)

Also consider state management needs. Components with shared state might need to be grouped under a common parent or use context.

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

List the types of components used when breaking down a UI.

A
  • Atomic components (buttons, inputs)
  • Molecules (form groups, card components)
  • Organisms (entire sections like a navigation bar)
  • Templates (page layouts)
  • Pages (complete views)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the key principles for managing component state and props?

A
  • Keep state as close as possible to where it’s needed
  • Lift state up when it needs to be shared
  • Use prop drilling for shallow component trees
  • Implement Context API for widely accessed state
  • Use Redux for complex global state
  • Apply immutability patterns when updating state
  • Destructure props for readability
  • Use TypeScript to enforce prop types
  • Provide default props when appropriate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which hooks are used most frequently in React?

A
  • useState
  • useEffect
  • useContext
  • useCallback
  • useMemo
  • useRef
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the useEffect hook do?

A

It runs a callback function after the component renders and re-runs when dependencies change.

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

What is the purpose of the cleanup function in useEffect?

A

To prevent memory leaks by disposing of resources created in the effect.

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

What strategies can be employed to prevent unnecessary re-renders in React?

A
  • Memoization techniques (React.memo(), useMemo(), useCallback())
  • State management optimization (keep state local, use immutable patterns)
  • Render optimization (virtualization, code-splitting)
  • Use profiling tools (React DevTools Profiler, Lighthouse audits)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When should you use useMemo?

A

When you have computationally expensive calculations that don’t need to be recalculated on every render.

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

What is the function of React.memo?

A

It optimizes pure components to prevent unnecessary re-renders based on the same props.

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

What is the purpose of the Intersection Observer API in lazy loading?

A

To load components as they enter the viewport.

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

What is Redux Toolkit (RTK) used for?

A

To simplify Redux development and reduce boilerplate code.

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

What does the createSlice function do in Redux Toolkit?

A

It generates actions and reducers, reducing boilerplate.

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

When should you use local component state over Redux?

A

When the state is only relevant to a single component and doesn’t need to be shared.

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

What is the role of createAsyncThunk in Redux Toolkit?

A

To handle asynchronous operations like API calls.

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

What are extraReducers used for in Redux Toolkit?

A

To handle pending, fulfilled, and rejected states for async operations.

17
Q

Fill in the blank: The _______ provides a consistent pattern for loading states and error handling in Redux Toolkit.

18
Q

What are the use cases for Context API in React?

A
  • State accessed by multiple components
  • Limited scope (theming, user preferences)
  • Changes infrequently
  • Small to medium complexity level