Hello I am new to react and I was wondering if I could return a function in a event handler instead of passing to the element a arrow function with parameter? Like this:
deletePersonHandler(person) {
return () => {
const persons = [...this.state.persons];
const index = persons.indexOf(person);
persons.splice(index, 1)
this.setState({ persons });
}
}
and on component render:
function Person(props) {
function Children() {
if (props.person.children) {
return <p>{props.person.children}</p>
}
else return null;
}
return (
<div className="person">
<p>Hello I am {props.person.name}, and I am {props.person.age} years old.</p>
<input value={props.person.name} type="text" onChange={(event) => { props.change(event, props.person.id) }} />
{/* Over Here!! */}
<button onClick={props.onDelete(props.person)}>Remove</button>
{Children()}
</div>
)
}
I tested the code and it runs fine but I'm asking if there are any setbacks or consequences? and if so what are the alternatives? Here is the full code's repository on my Github's account: https://github.com/EtshD1/react-components