A
A
Artur Kudashev2019-03-23 14:08:43
JavaScript
Artur Kudashev, 2019-03-23 14:08:43

How to manage the state of a child component?

Hello, I started to do a small project and a question arose. I don't know how to control the state of the child component from the parent. I have a click handler in the parent and it needs to change the state of the child component. How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-03-23
@archi_kud

does that mean?

onClick = e => {
  this.setState({daughter: 21})  // тут меняем стейт в родителе
}

....

<Something onClick={this.onClick} />   // тут "клик в родителя"
<DaughterComponent someValue={this.state.daughter} />   // это будет доступно в компоненте как props.someValue, и там уже рендерить как надо

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question