N
N
newUser20212021-04-10 23:57:25
React
newUser2021, 2021-04-10 23:57:25

How to call a child component method inside the parent component in react?

Hello. I'm new to development, learning react and would like to ask what is the right way in this solution?
I have a parent component that has a button. In the parent component, there is a child component that has its own method.
I'm trying to make it so that when you click on a button that is in the parent, the method that is in the chail is called.
Please tell me if it is possible to do this and whether it is right to do it, because I read that in React data is transferred from parent to child, and not from child to parent.
Thanks for the help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alice, 2021-04-11
@w3bsmes

Hold

K
Kirill Makarov, 2021-04-11
@kirbi1996

Pull the function to the parent, or use state managers

S
Steppp, 2021-04-11
@Steppp

Like throw props to the top?
app.js

const handlerClick = (value) => {
  console.log(value)
}
<Component handlerClick={handlerClick} />

Component.js
<button
  onClick={() => props.handlerClick('Клик по кнопке')}
>click<button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question