{"id":2393,"date":"2022-08-30T13:26:11","date_gmt":"2022-08-30T13:26:11","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2393"},"modified":"2022-08-30T13:26:11","modified_gmt":"2022-08-30T13:26:11","slug":"11-advanced-react-interview-questions-you-should-absolutely-know-with-detailed-answers","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2393","title":{"rendered":"11 Advanced React Interview Questions you should absolutely know (with detailed answers)"},"content":{"rendered":"\n<h1 id=\"47b0\">1. What is the React Virtual DOM?<\/h1>\n\n\n\n<p id=\"9ff1\"><strong>Virtual DOM<\/strong>&nbsp;is a concept where a&nbsp;<strong>virtual representation<\/strong>&nbsp;of the&nbsp;<strong>real DOM<\/strong>&nbsp;is kept inside the&nbsp;<strong>memory<\/strong>&nbsp;and is&nbsp;<strong>synced with the actual DOM<\/strong>&nbsp;by a library such as&nbsp;<strong>ReactDOM<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p id=\"5b5c\">The&nbsp;<strong>virtual DOM<\/strong>&nbsp;is an&nbsp;<strong>object<\/strong>&nbsp;that represents the&nbsp;<strong>real DOM<\/strong>&nbsp;in the memory. Since&nbsp;<strong>DOM<\/strong>&nbsp;updates are an integral part of any web app but are the&nbsp;<strong>costliest operation<\/strong>&nbsp;in the world of&nbsp;<strong>frontend<\/strong>, the&nbsp;<strong>virtual DOM<\/strong>&nbsp;is utilized to check for parts of the app that need to be updated &amp; update only those parts, thus&nbsp;<strong>significantly boosting performance<\/strong>.<\/p>\n\n\n\n<h1 id=\"736d\">2. Why do we need to&nbsp;<code>transpile<\/code>&nbsp;React code?<\/h1>\n\n\n\n<p id=\"9d16\"><strong>React<\/strong>&nbsp;code is written in&nbsp;<strong>JSX<\/strong>, but no browser can execute&nbsp;<strong>JSX<\/strong>&nbsp;directly as they are built to read-only regular&nbsp;<strong>JavaScript<\/strong>.<\/p>\n\n\n\n<p id=\"adc7\">Thus we require to use tools like&nbsp;<strong>Babel<\/strong>&nbsp;to transpile&nbsp;<strong>JSX<\/strong>&nbsp;to&nbsp;<strong>JavaScript<\/strong>&nbsp;so that the browser can execute it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1400\/0*dSaZZDejKKRE_X9h.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h1 id=\"9dcf\">3. What is the significance of&nbsp;<code>keys<\/code>&nbsp;in React?<\/h1>\n\n\n\n<p id=\"7ef1\"><code>Keys<\/code>&nbsp;in&nbsp;<strong>React<\/strong>&nbsp;is used to identify unique&nbsp;<strong>VDOM<\/strong>&nbsp;Elements with their corresponding data driving the&nbsp;<strong>UI<\/strong>; having them helps&nbsp;<strong>React<\/strong>&nbsp;optimize rendering by recycling existing&nbsp;<strong>DOM<\/strong>&nbsp;elements.<\/p>\n\n\n\n<p id=\"5e0e\"><code>Key<\/code>&nbsp;helps&nbsp;<strong>React<\/strong>&nbsp;identify which items have&nbsp;<strong>changed<\/strong>, are&nbsp;<strong>added<\/strong>, or are&nbsp;<strong>removed<\/strong>, enabling it to reuse already existing&nbsp;<strong>DOM<\/strong>&nbsp;elements, thus providing a&nbsp;<strong>performance boost<\/strong>.<\/p>\n\n\n\n<p id=\"3ace\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const Todos = ({ todos }) =&gt; {<br>  return (<br>    &lt;div&gt;<br>      {todos.map((todo) =&gt; (<br>        &lt;li&gt;{todo.text}&lt;\/li&gt;<br>      ))}<br>    &lt;\/div&gt;<br>  );<br>};<\/pre>\n\n\n\n<p id=\"8370\">This would cause new&nbsp;<strong>DOM Elements<\/strong>&nbsp;to be created everytime&nbsp;<code>todos<\/code>&nbsp;change, but adding the&nbsp;<code>key<\/code>&nbsp;prop (<code>&lt;li key={todo.id}&gt;{todo.text}&lt;\/li&gt;<\/code>) would result in &#8220;dragging&#8221; around the&nbsp;<strong>DOM Elements<\/strong>&nbsp;inside the&nbsp;<code>ul<\/code>&nbsp;tag &amp; updating only the necessary&nbsp;<code>li<\/code>s.<\/p>\n\n\n\n<h1 id=\"96f4\">4. What is the significance of&nbsp;<code>refs<\/code>&nbsp;in React?<\/h1>\n\n\n\n<p id=\"ad6a\"><code>Refs<\/code>&nbsp;are variables that allow you to&nbsp;<strong>persist data between renders<\/strong>, just like&nbsp;<code>state<\/code>&nbsp;variables, but unlike&nbsp;<code>state<\/code>&nbsp;variables, updating&nbsp;<code>refs<\/code>&nbsp;does NOT cause the&nbsp;<strong>component to re-render<\/strong>.<\/p>\n\n\n\n<p id=\"0ff6\"><code>Refs<\/code>&nbsp;are usually used to, but not restricted to, store reference to&nbsp;<strong>DOM elements<\/strong>.<\/p>\n\n\n\n<h1 id=\"f36c\">5. What are the most common approaches for styling a React application?<\/h1>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/440\/0*fPCATdXn2MyRFfuC.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h2 id=\"8a83\"><strong>CSS Classes<\/strong><\/h2>\n\n\n\n<p id=\"64ce\"><strong>React<\/strong>&nbsp;allows class names to be specified for a component like class names are set for a&nbsp;<strong>DOM<\/strong>&nbsp;element in&nbsp;<strong>HTML<\/strong>.<\/p>\n\n\n\n<p id=\"d3e8\">When developers first start using&nbsp;<strong>React<\/strong>&nbsp;after developing traditional web applications, they often opt for&nbsp;<strong>CSS classes<\/strong>&nbsp;as they are already familiar with the approach.<\/p>\n\n\n\n<h2 id=\"62f8\"><strong>Inline CSS<\/strong><\/h2>\n\n\n\n<p id=\"8cea\">Styling&nbsp;<strong>React<\/strong>&nbsp;elements using&nbsp;<strong>inline CSS<\/strong>&nbsp;allows styles to be completely scoped to an element. However,&nbsp;<strong>certain styling features are not available with inline styles<\/strong>. For example, the styling of&nbsp;<strong>pseudo-classes<\/strong>&nbsp;like&nbsp;<code>:hover<\/code>.<\/p>\n\n\n\n<h2 id=\"4e8f\"><strong>Pre-processors (Sass, Stylus, and Less)<\/strong><\/h2>\n\n\n\n<p id=\"e97e\"><strong>Pre-processors<\/strong>&nbsp;are often used on&nbsp;<strong>React<\/strong>&nbsp;projects. This is because, like&nbsp;<strong>CSS<\/strong>, they are well understood by developers and are often already in use if&nbsp;<strong>React<\/strong>&nbsp;is being integrated into a legacy application.<\/p>\n\n\n\n<h2 id=\"28dd\"><strong>CSS-in-JS Modules (Styled Components, Emotion, and Styled-jsx)<\/strong><\/h2>\n\n\n\n<p id=\"3785\"><strong>CSS-in-JS modules<\/strong>&nbsp;are a popular option for styling&nbsp;<strong>React<\/strong>&nbsp;applications because they integrate closely with&nbsp;<strong>React<\/strong>&nbsp;components. For example, they allow styles to change based on&nbsp;<strong>React<\/strong>&nbsp;props at runtime. Also, by default,&nbsp;<strong>most of these systems scope all styles to the respective component being styled<\/strong>.<\/p>\n\n\n\n<h1 id=\"18b2\">6. What are some of the performance optimization strategies for React?<\/h1>\n\n\n\n<h2 id=\"249d\"><strong>Using useMemo<\/strong><\/h2>\n\n\n\n<p id=\"8f65\"><code>useMemo<\/code>&nbsp;is a React hook that is used for&nbsp;<strong>caching CPU-Expensive functions<\/strong>. A&nbsp;<strong>CPU-Expensive function<\/strong>&nbsp;called repeatedly due to&nbsp;<strong>re-renders of a component<\/strong>, can lead to&nbsp;<strong>slow rendering<\/strong>.<\/p>\n\n\n\n<p id=\"c384\"><code>useMemo<\/code>&nbsp;hook can be used to&nbsp;<strong>cache<\/strong>&nbsp;such functions. By using&nbsp;<code>useMemo<\/code>, the&nbsp;<strong>CPU-Expensive function<\/strong>&nbsp;gets called only when it is needed.<\/p>\n\n\n\n<p id=\"f66f\"><code>useCallback<\/code>&nbsp;can be used to obtain a similar result.<\/p>\n\n\n\n<h2 id=\"2993\"><strong>Lazy Loading<\/strong><\/h2>\n\n\n\n<p id=\"711f\"><strong>Lazy loading<\/strong>&nbsp;is a technique used to&nbsp;<strong>reduce the load time of a React app<\/strong>. It helps reduce the risk of web app performances to a minimum, by&nbsp;<strong>loading up the components as the user navigates through the app<\/strong>.<\/p>\n\n\n\n<h1 id=\"fdf4\">7. What is prop drilling and how to avoid it?<\/h1>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/994\/0*Ypxno_eiUm56gEys.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"5b98\">Sometimes while developing&nbsp;<strong>React<\/strong>&nbsp;applications, there is a need to&nbsp;<strong>pass data from a component that is higher in the hierarchy<\/strong>&nbsp;to a&nbsp;<strong>component that is deeply nested<\/strong>. To pass data between such components, we pass props from a source component and&nbsp;<strong>keep passing the prop<\/strong>&nbsp;to the next component in the hierarchy&nbsp;<strong>till we reach the deeply nested component<\/strong>.<\/p>\n\n\n\n<p id=\"32bb\">The disadvantage of using&nbsp;<code>prop drilling<\/code>&nbsp;is that the components that should otherwise be not aware of the data have access to the data, moreover, the&nbsp;<strong>code becomes harder to maintain<\/strong>.<\/p>\n\n\n\n<p id=\"f044\"><code>Prop drilling<\/code>&nbsp;can be avoided using the&nbsp;<strong>Context API<\/strong>&nbsp;or some form of&nbsp;<strong>State Management<\/strong>&nbsp;library.<\/p>\n\n\n\n<h1 id=\"e9a5\">8. What is the&nbsp;<code>StrictMode<\/code>&nbsp;component and why would you use it?<\/h1>\n\n\n\n<p id=\"8faf\"><code>&lt;StrictMode \/&gt;<\/code>&nbsp;is a component included with&nbsp;<strong>React<\/strong>&nbsp;to provide&nbsp;<strong>additional visibility of potential issues<\/strong>&nbsp;in components. Suppose the application is running in&nbsp;<strong>development mode<\/strong>. In that case, any issues are logged to the&nbsp;<strong>development console<\/strong>, but these warnings are not shown if the application is running in&nbsp;<strong>production mode<\/strong>.<\/p>\n\n\n\n<p id=\"6ddf\">Developers use&nbsp;<code>&lt;StrictMode \/&gt;<\/code>&nbsp;to find problems such as&nbsp;<strong>deprecated lifecycle methods<\/strong>&nbsp;and&nbsp;<strong>legacy patterns<\/strong>, to ensure that all&nbsp;<strong>React<\/strong>&nbsp;components follow current best practices.<\/p>\n\n\n\n<p id=\"647e\"><code>&lt;StrictMode \/&gt;<\/code>&nbsp;can be&nbsp;<strong>applied at any level of an application component hierarchy<\/strong>, which allows it to be adopted incrementally within a codebase.<\/p>\n\n\n\n<h1 id=\"5b20\">9. What are&nbsp;<code>synthetic events<\/code>&nbsp;in React?<\/h1>\n\n\n\n<p id=\"14bc\"><code>Synthetic events<\/code>&nbsp;combine the response of different browser&#8217;s native events into one&nbsp;<strong>API<\/strong>, ensuring that the&nbsp;<strong>events are consistent across different browsers<\/strong>. The application is consistent regardless of the browser it is running in.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const Component = () =&gt; {<br>  const handleClick = (e) =&gt; {<br>    e.preventDefault(); \/\/ synthetic event<br>    console.log(\"link clicked\");<br>  };  return &lt;a onClick={(e) =&gt; handleClick}&gt;Click me&lt;\/a&gt;;<br>};<\/pre>\n\n\n\n<h1 id=\"d277\">10. Why is it not advisable to update&nbsp;<code>state<\/code>&nbsp;directly, but use the&nbsp;<code>setState<\/code>&nbsp;call?<\/h1>\n\n\n\n<p id=\"05a5\">The conventional way to update&nbsp;<code>state<\/code>&nbsp;is to use the&nbsp;<code>setState<\/code>&nbsp;call. Without using it, the user would still be able to modify the state, but it would not update the&nbsp;<strong>DOM<\/strong>&nbsp;to reflect the new state.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const Component = () =&gt; {<br>  const [count, setCount] = useState(0);<br>  \/\/ let [count, setCount] = React.useState(0);  const handleClickUpdate = () =&gt; {<br>    setCount((c) =&gt; c + 1);<br>    \/\/ count = count + 1; \/\/ will not update the DOM<br>  };  return (<br>    &lt;&gt;<br>      {count}<br>      &lt;button onClick={handleClickUpdate}&gt;Click me&lt;\/button&gt;<br>    &lt;\/&gt;<br>  );<br>};<\/pre>\n\n\n\n<h1 id=\"7e28\">11. What are&nbsp;<code>portals<\/code>&nbsp;in React?<\/h1>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1400\/0*Wlj0qnz_R-OFStz6.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"d9f0\"><strong>Portal<\/strong>&nbsp;is a recommended way to render children into a&nbsp;<strong>DOM<\/strong>&nbsp;node that exists outside the&nbsp;<strong>DOM hierarchy<\/strong>&nbsp;of the parent component.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const Portal = ({ children }) =&gt; {<br>  \/\/ NOTE: it is advisable to create a new DOM node for the portal<br>  const portalRoot = document.getElementById(\"portal-root\");  return ReactDOM.createPortal(children, portalRoot);<br>};<\/pre>\n\n\n\n<p id=\"9752\">Research says,&nbsp;<strong>writing down your goals on pen &amp; paper<\/strong>&nbsp;makes you&nbsp;<strong>21%<\/strong>&nbsp;to&nbsp;<strong>39%<\/strong>&nbsp;more likely to achieve them. Check out these notebooks and journals to&nbsp;<strong>make the journey of achieving your dreams easier<\/strong>:&nbsp;<a href=\"https:\/\/www.amazon.com\/Tapajyoti-Bose\/e\/B09VGDDHRR\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/www.amazon.com\/Tapajyoti-Bose\/e\/B09VGDDHRR<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is the React Virtual DOM? Virtual DOM&nbsp;is a concept where a&nbsp;virtual representation&nbsp;of the&nbsp;real DOM&nbsp;is kept inside the&nbsp;memory&nbsp;and is&nbsp;synced with the actual DOM&nbsp;by a library such as&nbsp;ReactDOM.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[132],"tags":[],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2393"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2393"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2393\/revisions"}],"predecessor-version":[{"id":2394,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2393\/revisions\/2394"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}