Answer the question
In order to leave comments, you need to log in
How to attach a listener to an existing ReactJS home element?
How to attach a listener to an existing ReactJS home element?
For example, I render radio buttons on the server. If the user clicks on one of them, I need to show the corresponding content, how can I attach a listener to it and pass them to react?
Answer the question
In order to leave comments, you need to log in
You can hang a listener like this:
A)
1) Write in the jsx tag
2) Write in the code or in the same ref:
But this is not the best practice.
B)ref={(elem)=>{this.elem = elem}}
class NavigationBlock extends Component {
constructor(props) {
super(props);
this.props=props;
this.handlerClick=this.handlerClick.bind(this);
}
handlerClick(e) {
//обработка клика
}
render() {
let emenets = bigArrayData.map((item)=>{
return <div onClick={this.handlerClick}>item</div>
});
return <div>{elements}</div>
}
}
e.target
. handlerClick(number) {
return (e)=>{
alert(number); //Типа обработка
}
}
render() {
let emenets = bigArrayData.map((item, i)=>{
return <div onClick={this.handlerClick(i)}>item</div>//вместо i любое значение
});
return <div>{elements}</div>
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question