1. What is React?
Ah, the classic opener.
If you don’t know this, just pack up and go home.
Kidding!
But seriously, React is a JavaScript library for building user interfaces.
It’s like the Lego of the web development world—snap components together to build something cool.
Example:
|
|
For more details, check out the React Wikipedia page.
2. What are Components?
Components are the building blocks of a React application. Think of them as JavaScript functions that return HTML (well, JSX, but let’s not split hairs).
Example:
|
|
3. What is JSX?
JSX stands for JavaScript XML. It’s a syntax extension that allows you to write HTML directly within JavaScript. It’s like mixing peanut butter and jelly—better together.
Example:
|
|
4. What are Props?
Props (short for “properties”) are like function arguments in JavaScript. They allow you to pass data from one component to another.
Example:
|
|
5. What is State?
State is like a component’s personal diary. It’s data that changes over time and affects what gets rendered.
Example:
|
|
6. What are Hooks?
Hooks are special functions that let you “hook into” React features. They’re like power-ups for your components.
Example:*
|
|
7. What is the Virtual DOM?
The Virtual DOM is React’s way of being sneaky efficient. It’s a lightweight copy of the actual DOM that React uses to see what needs updating without touching the real DOM unnecessarily.
8. What is Redux?
Redux is like a global state manager for your application. It helps you manage state outside of your components, making it easier to share data and debug.
Example:
|
|
9. What is Context API?
The Context API is React’s way of making sure you don’t have to pass props down like a hot potato through every component. It allows you to share values between components without explicitly passing props.
Example:
|
|
10. What is Prop Drilling?
Prop drilling is the process of passing data through multiple layers of components. It’s like playing telephone with your props—sometimes things get lost along the way.
Example:
|
|
11. React Server Components
React Server Components (RSC) are one of the hottest topics in modern React.
They allow React components to be rendered on the server and sent to the client as HTML, reducing JavaScript bundle sizes.
Example:
|
|
📖 Read more about it on React’s official site.
12. Suspense for Data Fetching
Suspense is React’s way of handling asynchronous operations like data fetching with built-in loading states.
Example:
|
|
This makes loading states easier than ever. No more isLoading
state variables everywhere!
13. Concurrent Rendering
React’s Concurrent Mode allows React to prepare multiple UI updates in the background without blocking the main thread.
Example scenario: Typing in a search box while data loads.
Example:
|
|
React will prioritize keeping the UI responsive over rendering heavy computations.
14. React.memo for Performance Optimization
When a component re-renders too much, it might be time to use React.memo()
. This prevents unnecessary renders by checking if props have changed.
Example:
|
|
If value
doesn’t change, the component won’t re-render.
15. useCallback and useMemo
These hooks help with performance optimizations when dealing with functions or computations inside components.
Example (useCallback):
|
|
Example (useMemo):
|
|
Both help avoid unnecessary recalculations.
16. Error Boundaries
React does not catch errors inside event handlers or asynchronous code. But with Error Boundaries, you can catch UI-breaking errors before they crash the entire app.
Example:
|
|
Wrap components inside <ErrorBoundary>
to catch errors gracefully.
17. Custom Hooks
If your components have too much repeated logic, create a custom hook instead.
Example:
|
|
Now, use it like this:
|
|
18. React Portal
Portals allow you to render components outside the root DOM node, useful for modals, tooltips, and dropdowns.
Example:
|
|
No more CSS z-index
nightmares!
19. Hydration in React
Hydration is when the server renders HTML first, and React takes over after the initial load.
Example:
|
|
It makes SSR (Server-Side Rendering) faster and avoids flickering during page loads.
20. React Fiber
React Fiber is the core engine behind React that allows concurrent rendering, time slicing, and improved performance.
While you won’t write Fiber directly, understanding it can help with debugging performance issues.
21. React Refs (useRef)
Ever wanted to interact directly with DOM elements in React without triggering a re-render?
Enter useRef()
!
Refs let you reference DOM nodes or persist values across renders without causing re-renders.
Example:
|
|
🔥 Tip: useRef()
is also great for storing mutable values between renders.
22. Controlled vs Uncontrolled Components
Ever heard of controlled components?
React wants all inputs to be controlled, meaning the state controls the value.
Example of a controlled component:
|
|
🛑 Uncontrolled components, on the other hand, don’t rely on state:
|
|
Controlled is React-y. Uncontrolled is old-school HTML form-y. Pick your poison.
23. Forwarding Refs (forwardRef
)
Refs are useful, but what if you need to pass a ref to a child component? Use forwardRef()
!
Example:
|
|
This is super useful when working with third-party UI libraries.
24. The useReducer
Hook
Tired of useState
? When state logic gets complex, useReducer()
comes to the rescue.
Example:
|
|
Think of useReducer()
as React’s mini Redux.
25. useLayoutEffect
vs useEffect
Both are similar, but useLayoutEffect()
fires before the browser paints the screen.
Good for synchronizing the DOM.
Example:
|
|
👀 Use useLayoutEffect
only when necessary, as it can block painting.
26. Context API Performance Issues
Context API is awesome, but overuse it, and your app slows down.
Solution: Memoize Context Values
|
|
Memoizing context prevents unnecessary re-renders.
27. Lazy Loading Components (React.lazy
)
Want to load components only when needed? Use React.lazy()
.
Example:
|
|
🚀 This improves performance by reducing initial load times.
28. Key Props in Lists Matter!
If you don’t use keys properly, React will re-render everything instead of just updating the changed items.
Good example:
|
|
Bad example:
|
|
Moral of the story: Always use stable, unique IDs for key
props.
29. Render Props
A technique that lets you share logic between components without inheritance.
Example:
|
|
Instead of passing JSX as children, we pass a function that returns JSX.
30. Strict Mode in React
React’s Strict Mode helps catch potential issues in your code. It doesn’t affect the UI but highlights side effects and warnings.
Example:
|
|
🚨 Using Strict Mode? Don’t freak out when your component mounts twice in development—it’s intentional.
31. Higher-Order Components (HOCs)
If you’ve ever needed to reuse logic across multiple components, HOCs are your friends. A Higher-Order Component is a function that takes a component and returns a new component with enhanced functionality.
Example:
|
|
When to use HOCs? When you find yourself repeating logic across multiple components (e.g., authentication, logging, styling). However, hooks have made HOCs less common.
32. React’s Event Delegation Magic
Ever wondered why React handles events differently than vanilla JavaScript?
Instead of attaching events directly to DOM elements,
React uses event delegation by binding all events to the root element (document
or root
div).
This means:
- Better performance (fewer event handlers)
- Works consistently across browsers
- Bubbling and capturing are still available
Example:
|
|
Even though the click event is on the button,
it’s actually handled by React’s synthetic event system.
33. React.lazy() for Component Code Splitting
We talked about React.lazy()
before, but did you know you can use it for code-splitting routes dynamically?
Example:
|
|
This way, React only loads components when needed, reducing initial load times.
34. Default Props vs. Optional Chaining
Ever seen this?
|
|
That’s default props in destructuring. But with optional chaining (?.
), you don’t always need defaults.
Example:
|
|
Both work, but optional chaining (?.
) is better when dealing with deeply nested objects.
35. The useImperativeHandle
Hook
What if you want to expose certain methods of a component but hide the rest? Use useImperativeHandle()
.
Example:
|
|
Now, only the focus()
method is exposed to the parent, keeping everything else private.
36. Why key
Matters in Lists
We know key
helps with performance, but did you know that using array indexes as keys can break UI state? 🤯
Bad Example:
|
|
If the array order changes, React will misinterpret UI updates, causing unexpected behavior.
Good Example:
|
|
Always use unique IDs for keys.
37. dangerouslySetInnerHTML
(And Why You Should Be Scared)
Need to inject raw HTML? Use dangerouslySetInnerHTML
(but carefully).
Example:
|
|
🚨 DANGER: This makes your app vulnerable to XSS (Cross-Site Scripting). Always sanitize user input!
38. React’s Hydration Concept
Hydration is when React reuses existing server-rendered HTML instead of re-rendering from scratch.
Example:
|
|
Why use it? SSR (Server-Side Rendering) with Hydration speeds up initial page load times.
39. The useTransition
Hook for Smooth UI Updates
If your UI is lagging due to expensive renders, useTransition()
can delay state updates for a smoother experience.
Example:
|
|
This keeps the UI responsive while state updates in the background.
40. Why You Should Memoize Event Handlers
If you’re passing functions as props, memoize them using useCallback()
to avoid unnecessary renders.
Example:
|
|
MANY of the Key Ideas Behind React :)
Concept | Description |
---|---|
Components | Reusable building blocks in React applications. |
JSX | Syntax extension allowing HTML within JavaScript. |
Props | Mechanism for passing data between components. |
State | Internal data management within a component. |
Hooks | Functions that let you use state and other React features in functional components. |
Virtual DOM | Lightweight copy of the DOM for efficient updates. |
Redux | State management library for JavaScript applications. |
Context API | Way to share values between components without passing props. |
Server Components | Render on the server, reducing client-side JS |
Suspense | Handle async data fetching elegantly |
Concurrent Rendering | Prioritize updates, making UI smooth |
React.memo | Prevent unnecessary renders |
useCallback/useMemo | Optimize functions and values |
Error Boundaries | Catch rendering errors gracefully |
Custom Hooks | Encapsulate reusable logic |
Portals | Render elements outside the root DOM node |
Hydration | Improve server-rendered pages |
React Fiber | The core of modern React |
useRef | Persist values across renders without causing re-renders |
Controlled Components | State-driven form inputs |
forwardRef | Pass refs to child components |
useReducer | Alternative to useState for complex logic |
useLayoutEffect | Runs before browser paint |
Context Performance | Memoization prevents unnecessary re-renders |
Lazy Loading | Load components only when needed |
Key Props | Stable keys prevent unnecessary re-renders |
Render Props | Function-based component logic reuse |
Strict Mode | Catches potential issues in development |
HOCs | Function that wraps and enhances a component |
Event Delegation | React handles events at the root level |
Code Splitting | Lazy-load components dynamically |
useImperativeHandle | Expose component methods selectively |
Hydration | Improves SSR performance |
useTransition | Smooth UI updates |