Answer the question
In order to leave comments, you need to log in
How to avoid bind in JSX?
I am newbie. I wonder how to avoid bind in the react code and do it differently so that the function is not re-created each time?
For example, here is the code in the render:
<i className="fa fa-trash-o icons" onClick={this.removeNote.bind(this, note.id)}/>
Answer the question
In order to leave comments, you need to log in
In your case, you can leave it as is. Can be changed like this:
class Example extends React.Component {
removeNote = () => {
};
render() {
const { note } = this.props;
return (
<i className="fa fa-trash-o icons" onClick={() => this.removeNote(note.id)}/>
);
}
}
class Example extends React.Component {
removeNote = e => {
const { id } = e.currentTarget.dataset;
};
render() {
const { note } = this.props;
return (
<i className="fa fa-trash-o icons" data-id={note.id} onClick={this.removeNote} />
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question