Skip to content

Instantly share code, notes, and snippets.

@tkh44
Created February 6, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkh44/83385f7f7a0803c018b52c482d96097a to your computer and use it in GitHub Desktop.
Save tkh44/83385f7f7a0803c018b52c482d96097a to your computer and use it in GitHub Desktop.

I was looking for a better way to compose multiple render prop based components. https://codesandbox.io/s/pm04m73n2x

const h = React.createElement;

const id = x => x;

const basicRenderCb = (result, Next) => // Next is the next component in the list.
  h(Next, result);

const L = [
  // Component, propsTransformer, customRenderCallback: (result, NextComponent) => React.Element
  [A, id, basicRenderCb],
  [B, id, basicRenderCb],
  [C, id, basicRenderCb]
];

const Y = (C1, [C2, pt, rt]) => p =>
  h(C2, pt(p), x => rt(x, C1));

const build = (...list) =>
  list.reverse().reduce(Y, M);

const R = build(...L);

render(
  <R name="⓵" />,
  document.getElementById("root")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment