<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Parwat Kunwar on Medium]]></title>
        <description><![CDATA[Stories by Parwat Kunwar on Medium]]></description>
        <link>https://medium.com/@parwatkunwar?source=rss-5872460bf399------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_QOjhvEk6pUKkkjtc3yoMg.jpeg</url>
            <title>Stories by Parwat Kunwar on Medium</title>
            <link>https://medium.com/@parwatkunwar?source=rss-5872460bf399------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:34:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@parwatkunwar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[this in React Components?]]></title>
            <link>https://medium.com/@parwatkunwar/this-in-react-component-9e5c588a7207?source=rss-5872460bf399------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e5c588a7207</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Parwat Kunwar]]></dc:creator>
            <pubDate>Sat, 26 Aug 2017 07:48:13 GMT</pubDate>
            <atom:updated>2018-05-19T18:42:51.961Z</atom:updated>
            <content:encoded><![CDATA[<pre>class HelloWorld extends React.Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = { message: &#39;Hi&#39; };<br>  }<br><br>  logMessage() {<br>    console.log(this.state.message);<br>  }<br><br>  render() {<br>    return (<br>      &lt;input type=&quot;button&quot; value=&quot;Log&quot; onClick={this.logMessage} /&gt;<br>    );<br>  }<br>}</pre><ol><li>this here represents the HelloWorld <em>component</em> context. So, the upper line of code should work as expected when we click the button. Nope, it won’t work and will through an error cannot read the property of undefined. But, the state is defined and it have the message property in it.</li><li>Probably you may know how to solve this issue, either you will use the bind to switch the context of this or use the =&gt; notation in function definition.</li><li>Ever Guessed, why you have to that, yes you might have, and the answer is to take the HelloWorld <em>component</em> context, But shouldn’t bethis auto-taking the <em>component</em> context. Nope, it doesn’t! But why?</li></ol><p>And this article is about that</p><blockquote>But shouldn’t be this auto-taking the component context?</blockquote><pre>/* this is line of code from HelloWorld Component */</pre><pre> &lt;input type=&quot;button&quot; value=&quot;Log&quot; onClick={this.logMessage} /&gt;<br></pre><blockquote>that above input tag which is JSX will be compiled to JS below</blockquote><pre>React.createElement(&#39;input&#39;, { onChange: this.logMessage, value: &quot;Log&quot; })</pre><blockquote>Remember if this is used inside an object method it always represents to the closet object.</blockquote><p>The below code</p><pre>{ onChange: this.logMessage, value: &quot;Log&quot; }</pre><p>is same as</p><pre>{ onChange: this.logMessage() { console.log(this.state.message) } , value: &quot;Log&quot; }</pre><pre>// let&#39;s assign it</pre><pre>let elephant = { onChange: this.logMessage() { console.log(this.state.message) } , value: &quot;Log&quot; }</pre><p>consoles undefined, cause this used in <em>console.log </em>inside logMessage() { console.log(<em>this</em>.state.message) } represents the elephant object not the HelloWorld <em>component </em>and there is no state property in it.</p><p>So the this used inside the logMessage will be the context of { onChange: this.logMessage, value: &quot;Log&quot; } object which is also declared as <em>elephant </em>object. To solve this issue we have to use bind to take the <em>HelloWorld</em> component context in this, or use =&gt; notation in function.</p><blockquote>This is mine first article on medium, if there are mistakes, feel free to response</blockquote><p><em>If you enjoyed this article, please help out your friends with a</em> 👏 <em>or a share.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e5c588a7207" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>