1. React State Management:
-
useStatevs.useReducer:-
useStateis useful for managing simple state values like numbers, strings, or objects. It's straightforward and works well for local state. -
useReduceris useful when state logic becomes complex or involves multiple sub-values. It also helps manage complex state transitions.
-
2. Performance Optimization:
-
Common Techniques:
-
Lazy loading components using
React.lazyandSuspenseto reduce the initial load time. -
Memoization using
useMemoandReact.memoto avoid unnecessary re-renders. -
Code splitting to load only the necessary components for specific routes.
-
-
React.memoanduseMemo:-
React.memois a higher-order component that prevents a component from re-rendering if its props haven’t changed. -
useMemomemoizes expensive computations and recalculates only when dependencies change.
-
3. React Hooks:
-
useEffectand Dependency Array:-
useEffectruns after the render and performs side effects like data fetching or updating the DOM.-
The dependency array determines when
useEffectis triggered. If the array is empty, it runs only once (on mount).
-
-
-
useRefvs.useState:-
useRefholds a mutable reference that persists across renders but doesn’t trigger a re-render when updated. -
useStateholds state that causes re-renders when updated.
-
4. Component Lifecycle in Functional Components:
-
Lifecycle Handling:
-
Functional components handle lifecycle methods using hooks:
-
useEffectmimicscomponentDidMount,componentDidUpdate, andcomponentWillUnmount.
-
-
-
useLayoutEffectvs.useEffect:-
useLayoutEffectruns synchronously after all DOM mutations and before the browser paints, which is useful for measuring layout changes. -
useEffectruns asynchronously after the DOM updates.
-
5. Routing:
-
Dynamic Routes in React Router:
-
Dynamic routes are created using
:paramNamein the path.You can access route parameters using the
useParamshook.
-
-
Nested Routes and Parameters:
-
You can define nested routes by using
<Outlet />in the parent component.
-
6. Forms and Input Handling:
-
Form Validation:
-
Libraries like Formik or React Hook Form help with form validation:
-
Formik provides easy state management and validation handling:
-
-
-
Controlled vs. Uncontrolled Inputs:
-
Controlled: State controls the input value.
-
Uncontrolled: The DOM manages the input value, accessed using
useRef.
-
7. Styling in React:
-
Ways to Style React Components:
-
CSS Modules: Scoped styles.
-
Styled-components: CSS-in-JS library.
-
Tailwind CSS: Utility-first CSS framework for consistent styling.
-
-
Conditional Styling:
-
You can conditionally apply styles based on state or props.
-
8. Testing in React:
-
Unit Tests with Jest/React Testing Library:
-
Use Jest for unit tests and React Testing Library for testing component behavior.
-
-
Shallow Rendering vs. Full DOM Rendering:
-
Shallow rendering renders only the component, without its child components. Useful for unit tests.
-
Full DOM rendering renders the component along with its children, simulating a more realistic DOM.
-
9. Custom Hooks:
-
Creating Custom Hooks:
-
Custom hooks allow you to extract reusable logic. For example, a
useFetchhook for fetching data:
-
-
Use Cases:
-
Fetching data, managing timers, or dealing with form validation logic.
-
10. State Management Libraries:
-
Redux with React:
-
Redux is a state management library that centralizes the state in a store.
Use
useSelectorto access state anduseDispatchto dispatch actions.
-
-
Redux Toolkit vs. React Context:
-
Redux Toolkit simplifies Redux logic with built-in best practices.
-
React Context is great for simple global state but becomes less efficient for complex state with frequent updates.
-
