0

I am aware what literal mean by void(0) || void 0

but i couldn't understand this snippet, i may not understanding void properly in context to strict mode

here is code in JSX

<button onClick={() => this.setState({ liked: true })}>
    Like
</button>

here is compiled code from babel which is equivalent to previous JSX snippet

"use strict";

var _this = void 0;

React.createElement("button", {
  onClick: function onClick() {
    return _this.setState({
      liked: true
    });
  }
}, "Like");

I feel like it is first set _this to undefined later at time of evaluation react-dom or reactjs does some kind of magic to convert it into this context.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hanzla Habib
  • 3,457
  • 25
  • 25
  • it is declaring an initializing a global variable with `undefined`. – Nina Scholz Aug 02 '20 at 08:06
  • @NinaScholz thanks for answering, Yes you are right, but how _this is evaluated here `return _this.setState({ liked: true });` – Hanzla Habib Aug 02 '20 at 08:23
  • the variable is assigned with a new value/object somwhere in the code. – Nina Scholz Aug 02 '20 at 08:28
  • 1
    If that's literally the entire file that you've passed through Babel, then it is correct: `this` is undefined and will remain undefined. If that JSX were inside an actual React component then the compilation result would be different, `this` would refer to the component. The context matters, you can't compile disconnected snippets of code and expect the same results as when compiling them as part of an actual project. – Guy Incognito Aug 02 '20 at 08:40
  • @NinaScholz thanks for the information, is there any possibility i get detailed information, how and which react dependency is responsible for handling `_this` assignment process, – Hanzla Habib Aug 02 '20 at 09:23
  • @GuyIncognito i understood your point, i was looking for bit inside from react core perspective, but thanks for your information – Hanzla Habib Aug 02 '20 at 09:26
  • I don't think you really understood it if you're asking for the "_this assignment process" (there is no such thing.) – Guy Incognito Aug 02 '20 at 09:28
  • @GuyIncognito sorry if you didn't get my point, i said its same as you mentioned first _this set to undefined and then somewhere in code it is set to `this` context of that specific component but any insight from core react perspective how they are handling these pieces thanks – Hanzla Habib Aug 02 '20 at 09:31
  • I didn't say that, I said the opposite. "then somewhere in code it is set to this context of that specific component" <–- that is *not* what happens. *The `this` context never changes*. – Guy Incognito Aug 02 '20 at 09:33
  • @GuyIncognito you must have read this statement `I feel like it is first set _this to undefined later at time of evaluation react-dom or reactjs does some kind of magic to convert it into this context.` i wanted to know how and which dependency managing this stuff – Hanzla Habib Aug 02 '20 at 09:33
  • Nothing manages that stuff because it doesn't happen. – Guy Incognito Aug 02 '20 at 09:34
  • @GuyIncognito i re-read your first message, now it makes sense thanks brother for taking time and responding to my query – Hanzla Habib Aug 02 '20 at 09:36

0 Answers0