Consider this React component:
import React, { Component } from 'react' class Frame extends Component { constructor(props) { super(props) console.log('constructor', this) // 我想要 *this* 但是 ... } render() { return <p></p> } } export default Frame
We initialize it like this:
const view = <Frame /> console.log(view) // 在这里!
These two print statements will output:
{$$typeof: Symbol(react.element), key: null, ref: null, props: {…}, type: ƒ, …} constructor Frame {props: {…}, context: undefined, refs: {…}, updater: {…}}
So, I actually want the pointer to this
in the constructor, but where I initialize the frame via JSX to the variable view
. is it possible?
Because the JSX
Your class component is initialized by React inside this function. React does not expose the value of<>
syntax is just a sugarcane forReact.createElement(Component, props, ...children)
this
, so the pointer to it cannot be obtained