{"componentChunkName":"component---src-templates-blog-post-js","path":"/react/practical-interview-questions/","result":{"data":{"site":{"siteMetadata":{"title":"Recipes Collection","author":"Internet Community"}},"markdownRemark":{"id":"d26df219-3a4d-5761-8f23-2f7d9b58e3a1","html":"<ul>\n<li>\n<p><a href=\"#practical-interview-questions\">Practical Interview Questions</a></p>\n<ul>\n<li><a href=\"#q---write-code-using-a-ref-hook-to-toggle-the-case-of-input-text-to-uppercase-and-lower-case-upon-clicking-a-button\">Q - Write code using a ref hook to toggle the case of input text to uppercase and lower case upon clicking a button.</a></li>\n<li><a href=\"#q---say-if-you-have-a-child-component-which-is-having-a-deep-tree-itself-if-we-want-to-prevent-unnecessary-rerendering-for-that-component-how-could-we-achieve-that\">Q - Say if you have a child component which is having a deep tree itself. If we want to prevent unnecessary rerendering for that component, how could we achieve that?</a></li>\n<li><a href=\"#q---say-i-need-to-derive-a-value-for-a-variable-by-doing-expensive-computation-and-i-need-to-show-this-variable-value-in-the-component-is-there-any-way-that-i-could-only-do-this-computation-once-and-use-it-with-next-rerenders\">Q - Say I need to derive a value for a variable by doing expensive computation, and I need to show this variable value in the component. Is there any way that I could only do this computation once and use it with next rerenders?</a></li>\n<li><a href=\"#q---say-if-i-want-to-load-a-particualr-page-component-related-javascript-code-on-demand-perhaps-when-user-is-navigating-to-that-page-itself-how-would-i-do-that\">Q - Say if I want to load a particualr page component related JavaScript code on demand. Perhaps when user is navigating to that page itself. How would I do that?</a></li>\n<li><a href=\"#q---say-i-want-to-show-a-counter-value-in-the-screen-and-this-value-should-be-incremented-every-second-and-upon-clicking-a-button-you-want-to-stop-this-counter-getting-incremented-automatically-how-would-you-do-this\">Q - Say I want to show a counter value in the screen and this value should be incremented every second. And upon clicking a button, you want to stop this counter getting incremented automatically. How would you do this?</a></li>\n</ul>\n</li>\n</ul>\n<h2 id=\"practical-interview-questions\"><a href=\"#practical-interview-questions\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Practical Interview Questions</h2>\n<h4 id=\"q---write-code-using-a-ref-hook-to-toggle-the-case-of-input-text-to-uppercase-and-lower-case-upon-clicking-a-button\"><a href=\"#q---write-code-using-a-ref-hook-to-toggle-the-case-of-input-text-to-uppercase-and-lower-case-upon-clicking-a-button\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Q - Write code using a ref hook to toggle the case of input text to uppercase and lower case upon clicking a button.</h4>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useRef<span class=\"token punctuation\">,</span> useState <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">\"react\"</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token string\">\"./App.css\"</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">App</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> inputField <span class=\"token operator\">=</span> <span class=\"token function\">useRef</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">const</span> <span class=\"token punctuation\">[</span>textCase<span class=\"token punctuation\">,</span> setTextCase<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">useState</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"lowercase\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">toggleInputCase</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">const</span> currentValue <span class=\"token operator\">=</span> inputField<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">.</span>value<span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>textCase <span class=\"token operator\">===</span> <span class=\"token string\">\"lowercase\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      inputField<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">.</span>value <span class=\"token operator\">=</span> currentValue<span class=\"token punctuation\">.</span><span class=\"token function\">toUpperCase</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token function\">setTextCase</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"uppercase\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n      inputField<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">.</span>value <span class=\"token operator\">=</span> currentValue<span class=\"token punctuation\">.</span><span class=\"token function\">toLowerCase</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token function\">setTextCase</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"lowercase\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>input</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>text<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">ref</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span>inputField<span class=\"token punctuation\">}</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>button<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">onClick</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span>toggleInputCase<span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n        Toggle Case\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n    </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> App<span class=\"token punctuation\">;</span></code></pre></div>\n<h4 id=\"q---say-if-you-have-a-child-component-which-is-having-a-deep-tree-itself-if-we-want-to-prevent-unnecessary-rerendering-for-that-component-how-could-we-achieve-that\"><a href=\"#q---say-if-you-have-a-child-component-which-is-having-a-deep-tree-itself-if-we-want-to-prevent-unnecessary-rerendering-for-that-component-how-could-we-achieve-that\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Q - Say if you have a child component which is having a deep tree itself. If we want to prevent unnecessary rerendering for that component, how could we achieve that?</h4>\n<p>shouldComponentUpdate method in class based components\nReact.memo in functional components</p>\n<h4 id=\"q---say-i-need-to-derive-a-value-for-a-variable-by-doing-expensive-computation-and-i-need-to-show-this-variable-value-in-the-component-is-there-any-way-that-i-could-only-do-this-computation-once-and-use-it-with-next-rerenders\"><a href=\"#q---say-i-need-to-derive-a-value-for-a-variable-by-doing-expensive-computation-and-i-need-to-show-this-variable-value-in-the-component-is-there-any-way-that-i-could-only-do-this-computation-once-and-use-it-with-next-rerenders\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Q - Say I need to derive a value for a variable by doing expensive computation, and I need to show this variable value in the component. Is there any way that I could only do this computation once and use it with next rerenders?</h4>\n<p>useMemo hook</p>\n<h4 id=\"q---say-if-i-want-to-load-a-particualr-page-component-related-javascript-code-on-demand-perhaps-when-user-is-navigating-to-that-page-itself-how-would-i-do-that\"><a href=\"#q---say-if-i-want-to-load-a-particualr-page-component-related-javascript-code-on-demand-perhaps-when-user-is-navigating-to-that-page-itself-how-would-i-do-that\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Q - Say if I want to load a particualr page component related JavaScript code on demand. Perhaps when user is navigating to that page itself. How would I do that?</h4>\n<p>React.lazy</p>\n<h4 id=\"q---say-i-want-to-show-a-counter-value-in-the-screen-and-this-value-should-be-incremented-every-second-and-upon-clicking-a-button-you-want-to-stop-this-counter-getting-incremented-automatically-how-would-you-do-this\"><a href=\"#q---say-i-want-to-show-a-counter-value-in-the-screen-and-this-value-should-be-incremented-every-second-and-upon-clicking-a-button-you-want-to-stop-this-counter-getting-incremented-automatically-how-would-you-do-this\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Q - Say I want to show a counter value in the screen and this value should be incremented every second. And upon clicking a button, you want to stop this counter getting incremented automatically. How would you do this?</h4>\n<ul>\n<li>Setup a setInterval timer in the useEffect method and assign the timer reference to a useRef</li>\n<li>Do clearInterval of the timer reference in the button click method</li>\n</ul>","timeToRead":3,"frontmatter":{"title":"Pracical Interview Questions","date":"April 24, 2022","spoiler":"Following are some notes collected through internet"},"fields":{"slug":"/react/practical-interview-questions/","langKey":"en"}}},"pageContext":{"slug":"/react/practical-interview-questions/","previous":{"fields":{"slug":"/react/react-architecture/","langKey":"en","directoryName":"react-architecture","maybeAbsoluteLinks":[]},"frontmatter":{"title":"React Architecuture"}},"next":{"fields":{"slug":"/react/concurrent-mode-and-suspense/","langKey":"en","directoryName":"concurrent-mode-and-suspense","maybeAbsoluteLinks":[]},"frontmatter":{"title":"Concurrent Mode and Suspense"}},"translations":[],"translatedLinks":[]}}}