Patternite Logo

Simplifying event handlers using currying in JavaScript, React

1

Repeated event handlers can often be condensed into a single handler through the use of currying

Loading...

Currying is a useful technique to consolidate event handlers that have very similar patterns, such as in our redirect example above. Rather than having to write two separate onClick handlers as in lines #29 and #32, we can use a curried function, handleClick. This function returns the actual click handler function, allowing an argument (type) to be passed.

JavaScript.info has a great, in-depth overview of function currying.

Profile picture for duncster

| edited

Patternite © 2021

Patternite Logo