The in-line anonymous function is fine in most cases, but if you want to not do that you can create a curried handler to attach.
const changeHandler = id => event => {...};
const clickHandler = index => () => {...};
This function consumes and encloses an id
variable and returns a function that consumes an event
object. This returned function is what elements/components will use as a callback.
Pass to component
<Persons click={clickHandler} change={changeHandler} />
And attach callback to handler
const Persons = (props) => {
return (props.persons.map((person, index) => {
return <Person
click={props.click(index)}
name={person.name}
age={person.age}
key={person.id}
changed={props.changed(person.id)} />
}));
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…