Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Last active January 12, 2017 13:12
Show Gist options
  • Save HugoDF/cb0c027acbb5ddc6159f650fe9149945 to your computer and use it in GitHub Desktop.
Save HugoDF/cb0c027acbb5ddc6159f650fe9149945 to your computer and use it in GitHub Desktop.
Example of partial application in JavaScript applied to React Component event handlers
class MyComponent extends React.Component {
partialHandleLinkClick(type, activeType){
return function(e) {
const hasKeyboardModifier = e.ctrlKey || e.shiftKey || e.altKey || e.metaKey;
updateType(type, activeType, hasKeyboardModifier);
};
}
render() {
const types = [ 'Foo', 'Bar', 'Baz' ];
return (
<div>
{
types.map( (type, i) => {
<a key={i} href="#"
onClick={this.partialHandleLinkClick(type, this.props.activeType)}>
{type}
</a>
})
}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment