<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MianUmair</title>
    <description>The latest articles on DEV Community by MianUmair (@amianumair).</description>
    <link>https://dev.to/amianumair</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F929960%2F13579a2c-65e1-46d1-a09c-43ea115d87c5.jpeg</url>
      <title>DEV Community: MianUmair</title>
      <link>https://dev.to/amianumair</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amianumair"/>
    <language>en</language>
    <item>
      <title>Top 5 Most Asked React Interview Questions and Answers By Muhammad Umair</title>
      <dc:creator>MianUmair</dc:creator>
      <pubDate>Wed, 21 Sep 2022 08:58:28 +0000</pubDate>
      <link>https://dev.to/amianumair/top-5-most-asked-react-interview-questions-and-answersby-muhammad-umair-k6f</link>
      <guid>https://dev.to/amianumair/top-5-most-asked-react-interview-questions-and-answersby-muhammad-umair-k6f</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yrpx2WA4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8aqeuvq9nmq4dpp7yet.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yrpx2WA4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8aqeuvq9nmq4dpp7yet.jpg" alt="Top 5 Most Asked React Interview Questions and Answers&amp;lt;br&amp;gt;
By Muhammad Umair" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Top 5 Most Asked React Interview Questions and Answers&lt;/p&gt;

&lt;h2&gt;
  
  
  Question no 1: Why should we not update the state directly?
&lt;/h2&gt;

&lt;p&gt;If you try to update the state directly then it won’t re-render the component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Wrong this.state.message = 'Hello world'&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Instead, use setState() method. It schedules an update to a component's state object. When the state changes, the component responds by re-rendering.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//Correct this.setState({ message: 'Hello World' })&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Note: You can directly assign to the state object either in the constructor or using the latest javascript’s class field declaration syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question no 02: What is the purpose of the callback function as an argument of setState()?
&lt;/h2&gt;

&lt;p&gt;The callback function is invoked when setState is finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.&lt;/p&gt;

&lt;p&gt;Note: It is recommended to use the lifecycle method rather than this callback function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setState({ name: 'John' }, () =&amp;gt; console.log('The name has updated and component re-rendered'))&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question no 3: What is the difference between HTML and React event handling?
&lt;/h2&gt;

&lt;p&gt;Below are some of the main differences between HTML and React event handling,&lt;/p&gt;

&lt;p&gt;In HTML, the event name usually represents in lowercase as a convention:&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;Whereas in React it follows the camelCase convention:&lt;/p&gt;

&lt;p&gt;jsx harmony &lt;/p&gt;

&lt;p&gt;In HTML, you can return false to prevent default behavior:&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;Whereas in React you must call preventDefault() explicitly:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function handleClick(event) {    &lt;br&gt;
event.preventDefault()&lt;br&gt;
console.log('The link was clicked.')&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer "activateLasers" function in the first point for example)&lt;/p&gt;

&lt;h2&gt;
  
  
  Question no 4: How to bind methods or event handlers in JSX callbacks?
&lt;/h2&gt;

&lt;p&gt;There are 3 possible ways to achieve this:&lt;/p&gt;

&lt;p&gt;Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies to React event handlers defined as class methods. Normally we bind them in the constructor.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Foo extends Component { &lt;br&gt;
constructor(props) {   &lt;br&gt;
super(props);   &lt;br&gt;
this.handleClick = this.handleClick.bind(this);&lt;br&gt;
} &lt;br&gt;
handleClick() {   &lt;br&gt;
console.log('Click happened');&lt;br&gt;
} &lt;br&gt;
render() {   &lt;br&gt;
return &amp;lt;button onClick={this.handleClick}&amp;gt;Click Me&amp;lt;/button&amp;gt;;&lt;br&gt;
} }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Public class fields syntax: If you don’t like to use the bind approach then public class fields syntax can be used to correctly bind callbacks.&lt;/p&gt;

&lt;p&gt;`jsx harmony handleClick = () =&amp;gt; { console.log(‘this is:’, this) }&lt;/p&gt;

&lt;p&gt;jsx harmony  {'Click me'} &lt;br&gt;
`&lt;br&gt;
Arrow functions in callbacks: You can use arrow functions directly in the callbacks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jsx harmony handleClick() {&lt;br&gt;
console.log('Click happened');&lt;br&gt;
} render() {&lt;br&gt;
return &amp;lt;button onClick={() =&amp;gt; this.handleClick()}&amp;gt;Click Me&amp;lt;/button&amp;gt;;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: If the callback is passed as a prop to child components, those components might do an extra re-rendering. In those cases, it is preferred to go with .bind() or public class fields syntax approach considering performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question no 5: How to pass a parameter to an event handler or callback?
&lt;/h2&gt;

&lt;p&gt;You can use an arrow function to wrap around an event handler and pass parameters:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jsx harmony &amp;lt;button onClick={() =&amp;gt; this.handleClick(id)} /&amp;gt;&lt;br&gt;
This is an equivalent to calling '.bind':  &lt;br&gt;
jsx harmony &amp;lt;button onClick={this.handleClick.bind(this, id)} /&amp;gt;&lt;br&gt;
Apart from these two approaches, you can also pass arguments to a function which is defined as arrow function jsx harmony &amp;lt;button onClick={this.handleClick(id)} /&amp;gt; handleClick = (id) =&amp;gt; () =&amp;gt; {&lt;br&gt;
console.log("Hello, your ticket number is", id)&lt;br&gt;
};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Please do Like, follow me on Here and on Medium &lt;br&gt;
&lt;a href="https://medium.com/@amianumair"&gt;https://medium.com/@amianumair&lt;/a&gt;&lt;br&gt;
for more articles like this.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
