Skip to content
This repository was archived by the owner on Feb 24, 2020. It is now read-only.

API: First-class modules for components #28

Merged
merged 22 commits into from
Nov 28, 2018

Conversation

bryphe
Copy link
Member

@bryphe bryphe commented Nov 28, 2018

Issue: Currently, we have no way to distinguish whether a component came from the same 'component class' today. This is important when determining whether to reuse state/effects. #26 went a step forward but the API is a bit clunky:

let componentWrappingB = (~children, ~x, ()) =>
  TestReact.component(() => <bComponent />, 
  ~uniqueId="componentWrappingB",
  ~children);

I'm proposing moving to a new API for creating components, using first-class modules - a function that returns a module.

This allows us to do the following:

  • Implicitly create a unique class id, instead of leaving that burden on the user
  • Implement custom components in an 'uppercase' convention, which seems to be a dominant style in the React ecosystem.

The new syntax would be:
module ComponentWrappingB =
(val component((render, ~x, ~children, ()) =>
render(() => {

}, ~children);
));

The component function creates the module (val unpacks it), and there would be a render function passed to the component.

And could be used as <ComponentWrappingB x />.

The anatomy of the function would be:

module ComponentWrappingB = 
    (val component((render, /* ...props */, ~children, ()) => 
        render(() => {
            /* ...any hooks here... */

            /* ...return another custom component / primitive component */
            <aComponent testVal=x/>
        }, ~children);
));

I think this is a step in a better direction, but I'd be interested to see if there is a way we could scrap that render function. A potential point of confusion I could see is perhaps users putting hooks outside the render block - we should make sure that throws.

@bryphe bryphe changed the title [WIP] API: First-class modules for components aPI: First-class modules for components Nov 28, 2018
@bryphe bryphe changed the title aPI: First-class modules for components API: First-class modules for components Nov 28, 2018
@bryphe bryphe merged commit cc0dbb4 into master Nov 28, 2018
@bryphe bryphe deleted the bryphe/api/first-class-module-api branch November 28, 2018 17:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant