, , , React.PureComponent useMemo. (Lambda) prop ( JSX), , JS . , , JavaScript .
, . :
customers.map( c => <Btn onClick={ () => this.deleteCust(c.id) } /> );
customers.map( c => <Btn onClick={ this.deleteCust.bind(this, c.id) } /> );
customers.map( c => <Btn onClick={ this.deleteCust.call(this, c.id) } /> );
customers.map( c => <Btn onClick={ this.deleteCust.apply(this, [c.id]) } /> );
, . :
const Btn = ({ clicked, customer }) => {
const buttonClickHandler = () => {
clicked(customer.id)
}
return <button onClick={buttonClickHandler}>Click me!</button>
}
const App = () => {
return (
<App>
{ customers.map(c => <Btn customer={c} clicked={deleteCust} />) }
</App>
)
}
So now, because instead of using an anonymous function (which cannot be reused), we use a function expression in a constant, React does not recreate a new function every time a new component is rendered, and the garbage collector can be collected in parts!
source
share