P
P
Pavel Didenko2020-01-20 17:32:22
React
Pavel Didenko, 2020-01-20 17:32:22

How to properly forward forward props in state?

How to correctly forward props to state?
I use class field syntax and do it like this:

state = {
  text: this.props.text
};

Here I heard an opinion from one developer that this is wrong, like "this should be inside a non-static method" and needs to be initialized in the constructor. He is right? state in what way does it relate to this, is it not a method?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mikhail Osher, 2020-01-20
@Dasslier

Here I heard an opinion from one developer that this is wrong, like "this should be inside a non-static method" and needs to be initialized in the constructor. He is right?

That's right. Only he does not understand that such a record is not static, and after the transpile it will end up in the constructor.

R
Roman Alexandrovich, 2020-01-20
@RomReed

constructor(props){
super(props)
this.state={
  text: this.props.text
}
}

this is how it should be. But if your method works, then why not use it?))))

M
Marat Nagayev, 2020-01-20
@nagayev

In general, classes are like that, there are React Hooks . There's no need to write this in principle.

R
real2210, 2020-01-21
@real2210

In general, logically, you need to use props directly, the state stores only what can change during the operation of the component itself with the same props

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question