3

How can be possible that 'this' in React.Component life cycle have changed to different object? In render() and in componentDidMount() this points to different objects?

  1. I use React 16.0.0
  2. All life-cycle methods called only once
  3. I transpile Typescript with Webpack
  4. I couldn't repeat it in a browser, in browser one === two // true
  5. I have very big app, so I thought it can be ReactFiber some kind of optimization?

Like here: JsBin

This is changed enter image description here

Mihey Mik
  • 1,643
  • 13
  • 18

2 Answers2

0

As you can see, the two has more properties. It's because at first output in constructor it's a pure Javascript Class. And then, React comes along and alter the Class before, during and after the render.

ChrisR
  • 3,922
  • 1
  • 14
  • 24
  • Well, get it, but this doesn't answer my question, why this changes to different object – Mihey Mik Mar 18 '18 at 05:22
  • It **is** the same object, it just doesn't have the same properties after being altered by React. So the `===` strict equality expression returns false. – ChrisR Mar 19 '18 at 08:46
  • this isn't how js `===` operator works. the object is same when variable pointers to the same object from left and right sides of `===` – Mihey Mik Mar 19 '18 at 10:51
0

Take this example:

var a = {one:"1"};

var b = a;

a==b  // true
a===b // true
var c = Object.assign({}, a);

c     //{one: "1"}
a===c // false
a==c  // false

The call to Object.assign created a copy of the object thus inequality.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
  • Lukasz this doesn't answer my question. `Object.assign` - assigns new properties to the first argument. In my case is 'window' object. So this lets me aterwards in the console see `one` and `two` values – Mihey Mik Mar 18 '18 at 05:24