March 15, 2019

Higher order component for ReasonReact

A higher order component is basically a reusable component which allows you to takes in another component and return a new component.

To recap about higher component check our my older post here

In ReasoML higher order function are called "Functors" which takes in a module type and returns a module type, functors are written like function, except you use module instead of const/let/var...

lets try to write a simple functor here:

  module type PersonType = {
    type t;

    let fullName: (t, t) => string;
  };

  module MakeName = (T: PersonType) => {
    type name = T.t;

    let sayName = (x, y) => "Hello I am " ++ T.fullName(x, y);
  };

  module NameString = {
    type t = string;

    let fullName = (x, y) => x ++ y;
  };

  module Max = MakeName(NameString)

  Js.log(Max.sayName("max", "li"));

You can read read more about Functor here

So now we want to use this approach on higher order component, kind of like react, wrap component in a function, here wrap in a functor.

module type S = {
  type t;

  let equal: (t, t) => bool;
};

module SelectHoc = (T:S) => {
  type t = {
    label: string,
    value: T.t
  };

  let component = ReasonReact.statelessComponent("selectComponent");

  let make = (
    ~items: array(t),
    ~selectedItem,
    _children
  ) => {
    ...component,
    render: (_self) => {

    }
  }
};

module SelectInt =
  SelectHoc({
    type t = string;

    let equals = (x, y) => x == y;
  });

/* use the component like this... */
let items = [|
  {Int.label: "first", value: 1},
  {label: "second", value: 2},
  {label: "third", value: 3},
  {label: "fourth", value: 4},
|];

Top 6 Most Recommended Developer Books

The Pragmatic Programmer

by Dave Thomas, Andy Hunt

Check out this book on Amazon)}

The Pragmatic Programmer is one of those rare tech audiobooks you'll listen, re-listen, and listen to again over the years. Whether you're new to the field or an experienced practitioner, you'll come away with fresh insights each and every time. Dave Thomas and Andy Hunt wrote the first edition of this influential book in 1999 to help their clients create better software and rediscover the joy of coding. These lessons have helped a generation of programmers examine the very essence of software development, independent of any particular language, framework, or methodology, and the Pragmatic philosophy has spawned hundreds of books, screencasts, and audio books, as well as thousands of careers and success stories. Now, 20 years later, this new edition re-examines what it means to be a modern programmer. Topics range from personal responsibility and career development to architectural techniques for keeping your code flexible and easy to adapt and reuse.

Published: 2019

Genre: Programming

Cover of The Pragmatic Programmer

The Pragmatic Programmer

Cover of Clean Code: A Handbook of Agile Software Craftsmanship

Clean Code: A Handbook of Agile Software Craftsmanship

Cover of Working Effectively with Legacy Code

Working Effectively with Legacy Code

Cover of Introduction to Algorithms

Introduction to Algorithms

Cover of Eloquent JavaScript

Eloquent JavaScript

Cover of The Road to React

The Road to React