G
G
Governor2021-11-09 16:50:10
JavaScript
Governor, 2021-11-09 16:50:10

How is the this.props property available in the class bean's render method?

While studying react, I ran into an illogical situation. In the method , the renderproperty is available, this.propsbut in all other methods of the class, this property is not available.
It begs the question, where did it go?
If, according to the class logic, there is a property available in the method this, it must have been initialized by someone, but I can’t get it even in the constructor.

It turns out that in fact there is none this.props, and is it such syntactic sugar at the level balel?

I made a mistake, this.props is available in the constructor.

More

Когда в конструкторе логирую так:
console.log(this);
Вижу объект класса вместе с props.

Но когда пишу так:
console.log(`props: ${JSON.stringify(this.props)}`);

Получаю undefined

618a83b87347b018936817.png
На картинке на первой строке видно серым напиано props: undefined.
На строке ниже развернутые свойства объекта, и там уже есть свосто props с объектом:
{id: 1, name: 'haha', important: true}

Весь код конструктора:
constructor() {
  super();

  console.log(`props: ${JSON.stringify(this.props)}`);
  console.log(``);
  console.log(this);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DimaIs, 2021-11-09
@DimaIs

1. 2.
class AnyCompnent extends React.Component

constructor(props) {
  super(props);
}

Read about class inheritance in JS: link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question