Answer the question
In order to leave comments, you need to log in
How to handle click on each Counter in each child component?
Greetings!
You need to create a counter and listen for it to change in each individual child component (counter is an example https://reactjsexample.com/react-counter/ ).
Ready counter is not interested, because I want to understand exactly the algorithm for solving the current problem ...
For clarity, I sketched a simple example:
function Parent() {
function dec() { console.log("decrement"); }
function inc() { console.log("increment"); }
return(
<div className="childs">
{[1, 2, 3].map((data, i) => {
return <Child key={i} dec={dec} inc={inc} />
})}
</div>
);
}
function Child(props) {
return(
<div className="child">
<div className="child__decrement">-</div>
<input type="number" className="child__input" defaultValue="0"/>
<div className="child__increment">+</div>
</div>
);
}
.child__decrement
it is necessary to increase the value of .child__input
, and to decrease when clicking on .child__increment
. Please note that there will be several such child components. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question