Skip to content

Instantly share code, notes, and snippets.

@coryhouse
Last active July 3, 2017 21:44
Show Gist options
  • Save coryhouse/a002111d01fb7ee1862af55d4a26abfa to your computer and use it in GitHub Desktop.
Save coryhouse/a002111d01fb7ee1862af55d4a26abfa to your computer and use it in GitHub Desktop.
Example of extracting a child component to avoid binding in render
import React from 'react';
import PropTypes from 'prop-types';
class UserListItem extends React.Component {
onDeleteClick = () => {
// No bind needed since we can compose
// the relevant data for this item here
this.props.onClick(this.props.user.id);
}
// No arrow func in render! 👍
render() {
return (
<li>
<input
type="button"
value="Delete"
onClick={this.onDeleteClick}
/>
{this.props.user.name}
</li>
);
}
}
UserListItem.propTypes = {
user: PropTypes.object.isRequired,
onClick: PropTypes.func.isRequired
};
export default UserListItem;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment