React Basics Flashcards
Basic React Concepts
What is the primary reason for preferring functional components over class components in React?
Since the introduction of hooks (16.8), functional components can do everything class components can do with simpler syntax and better performance characteristics.
- Easier to test
- Less boilerplate
- more declarative
What principle do you follow when breaking down a complex UI into components?
I follow the single responsibility principle.
- Atomic components (buttons, inputs)
- Molecules (form groups, card components)
- Organisms (entire sections like nav bars)
- Templates (page layouts)
- Pages (complete views)
Also consider state management needs. Components with shared state might need to be grouped under a common parent or use context.
List the types of components used when breaking down a UI.
- Atomic components (buttons, inputs)
- Molecules (form groups, card components)
- Organisms (entire sections like a navigation bar)
- Templates (page layouts)
- Pages (complete views)
What are the key principles for managing component state and props?
- 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
Which hooks are used most frequently in React?
- useState
- useEffect
- useContext
- useCallback
- useMemo
- useRef
What does the useEffect hook do?
It runs a callback function after the component renders and re-runs when dependencies change.
What is the purpose of the cleanup function in useEffect?
To prevent memory leaks by disposing of resources created in the effect.
What strategies can be employed to prevent unnecessary re-renders in React?
- 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)
When should you use useMemo?
When you have computationally expensive calculations that don’t need to be recalculated on every render.
What is the function of React.memo?
It optimizes pure components to prevent unnecessary re-renders based on the same props.
What is the purpose of the Intersection Observer API in lazy loading?
To load components as they enter the viewport.
What is Redux Toolkit (RTK) used for?
To simplify Redux development and reduce boilerplate code.
What does the createSlice function do in Redux Toolkit?
It generates actions and reducers, reducing boilerplate.
When should you use local component state over Redux?
When the state is only relevant to a single component and doesn’t need to be shared.
What is the role of createAsyncThunk in Redux Toolkit?
To handle asynchronous operations like API calls.
What are extraReducers used for in Redux Toolkit?
To handle pending, fulfilled, and rejected states for async operations.
Fill in the blank: The _______ provides a consistent pattern for loading states and error handling in Redux Toolkit.
RTK Query
What are the use cases for Context API in React?
- State accessed by multiple components
- Limited scope (theming, user preferences)
- Changes infrequently
- Small to medium complexity level