Last active
July 3, 2017 21:44
-
-
Save coryhouse/a002111d01fb7ee1862af55d4a26abfa to your computer and use it in GitHub Desktop.
Example of extracting a child component to avoid binding in render
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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