Speaker Deck

Higher Order Components in React at Voxxed Days Ticino 2016

by Matteo Ronchi

Published April 30, 2016 in Programming

see code at https://gist.github.com/cef62/23b37d0fb8b47c0ed9c5a40a0ae15414

React is “a Javascript library for building user interfaces”. The core of React follows many concepts of Functional Programming, code composition is one of its main features. In React there are 2 main types of building blocks: stateful components and stateless components. The former type is defined by elements owning the application logic; this kind of components often is bound to external elements and owns a state. The latter type is composed of pure components where all dynamic data is received from their props and no internal state is defined. This approach often produces replication of functionalities in several components; Higher Order Components (HoC) is the natural solution for this scenario and is a more functional alternative to traditional React mixins. An HoC abstracts component functionalities and offers an easy way to compose them when required making the code more maintainable and avoiding duplicates. During the session we will see how to implements this pattern taking attention on its pros and cons.