I made a dummy React component that does nothing but prints out its this
value.
import React from 'react'
import ReactDOM from 'react-dom'
class MyComponent extends React.Component {
constructor() {
super()
}
componentDidMount() {
console.log(this)
}
render() {
return <div />
}
}
const myComponent = new MyComponent()
console.log(myComponent)
ReactDOM.render(<MyComponent />, document.getElementById('app'))
I expected that console.log(myComponent)
would produce the same result as console.log(this)
. However, the latter has additional properties (e.g. _reactInternalFiber
) and some properties are different (e.g. context
, props
).
My question is, how is React or ReactDOM able to provide these additional attributes? You would think that all instances of MyComponent
would have the same attributes since they have the same constructor()
function, but that is not the case here.