React PDF 1
React PDF 1
By Ankit Sharma
linkedin.com/in/ankit7rma/
JSX is a syntax extension for JavaScript that allows you to write HTML-like code within
JavaScript. It gets transformed into regular JavaScript objects and function calls during
compilation. JSX makes React code more readable and writing templates more intuitive.
Components are reusable, self-contained pieces of UI that can accept inputs (props) and
return React elements. They can be either function components (modern approach) or
class components (traditional approach). Components help in building modular,
maintainable applications.
Props are read-only inputs passed to components from their parents, while state is internal,
mutable data managed by the component itself. Props flow down, state is contained. Use
props for component configuration, state for interactive data that changes over time.
Hooks are functions that allow you to use state and other React features in function
components. Common hooks include useState (state management), useEffect (side
effects), useContext (context consumption), and useRef (mutable references). They must
be called at the top level of components.
Question 5: What is the Virtual DOM?
The Virtual DOM is a lightweight copy of the actual DOM in memory. React uses it to
optimize rendering by first making changes to this virtual representation, then efficiently
updating only the necessary parts of the real DOM through a process called reconciliation.
The dependency array in useEffect determines when the effect should run. Empty array ([])
means run once on mount, no array means run on every render, and array with
dependencies means run when those values change. It's crucial for optimizing
performance and preventing infinite loops.
Question 7: What are React Keys and why are they important?
Keys are special attributes used to help React identify which items have changed, been
added, or been removed in lists. They should be stable, unique identifiers. Keys improve
performance by allowing React to update only the necessary items instead of re-rendering
the entire list.
Context provides a way to pass data through the component tree without having to pass
props manually at every level. It's useful for sharing global data like themes, user
authentication, or language preferences. However, it shouldn't be used for every state
management need.
Code splitting is a technique to split your code into small chunks and load them on
demand. React.lazy() and Suspense enable component-level code splitting. This improves
initial load time by only loading the necessary code for the current view.
Question 10: Explain React's Error Boundaries
Error Boundaries are components that catch JavaScript errors in their child component
tree, log errors, and display fallback UI. They prevent the entire app from crashing due to
errors in one component. They must be class components using specific lifecycle methods.
Portals provide a way to render children into a DOM node that exists outside the DOM
hierarchy of the parent component. Common uses include modals, tooltips, and floating
menus. They maintain event bubbling through the React tree regardless of actual DOM
position.
SSR is rendering React components on the server rather than in the browser. Benefits
include better SEO, faster initial page load, and improved performance on low-powered
devices. Next.js is a popular framework for implementing SSR in React applications.
Custom Hooks are JavaScript functions that start with 'use' and may call other hooks. They
allow you to extract component logic into reusable functions, share stateful logic between
components, and keep components clean and DRY (Don't Repeat Yourself).
Question 15: How do you optimize React performance?