Answer the question
In order to leave comments, you need to log in
What will happen with ES6?
Vorpos on this code. How to write in onDelete = this.props.onNoteDelete through ES6 so that the context is not lost?
render() {
let onNoteDelete = this.props.onNoteDelete;
return(
<div className="notes-grid" ref="grid">
{this.props.notes.map(note => {
return (
<Note
key={note.id}
color={note.color}
onDelete={onNoteDelete.bind(null, note)}> // и эта строка
{note.text}
</Note>);
})
}
</div>
)
}
Answer the question
In order to leave comments, you need to log in
You can do this:
In the component from which you pass the handler, define it as an inline arrow class function :
class Parent {
onNoteDelete = note => {
// some stuff
};
}
render() {
const { notes, onNoteDelete } = this.props;
return(
<div className="notes-grid" ref="grid">
{notes.map(note => (
<Note
key={note.id}
color={note.color}
onDelete={() => onNoteDelete(note)}
>
{note.text}
</Note>
))}
</div>
)
}
render() {
let onNoteDelete = this.props.onNoteDelete;
return(
<div className="notes-grid" ref="grid">
{this.props.notes.map(note => {
return (
<Note
key={note.id}
color={note.color}
onDelete={()=>onNoteDelete.bind(null, note)}> // и эта строка
{note.text}
</Note>);
})
}
</div>
)
}
constructor(props){
super(props)
this.onNoteDelete=this.onNoteDelete.bind(this)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question