Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Use h() instead of htm for components #88

Open
pastelmind opened this issue Sep 7, 2020 · 0 comments
Open

Use h() instead of htm for components #88

pastelmind opened this issue Sep 7, 2020 · 0 comments
Assignees
Labels
type: enhancement New feature or request

Comments

@pastelmind
Copy link
Owner

pastelmind commented Sep 7, 2020

htm allows us to write JSX-like code within tagged template literals, removing the need for JSX transpilation. Unfortunately, it does not support TypeScript very well. I cannot use TypeScript to verify the props and return types of my custom components.

function MyComponent(props) {
  // propA, propB are not type-checked
  return html`
    <div class="my-component">
      <${InnerComponent} propA="foo" propB=${bar} />
    </div>
  `;
}

The h() function is properly typed. Maybe I could use it instead?

Some options:

Write JSX and transpile with TypeScript

No-go, since one of this project's major goal is to write JS code that just runs with no transpilation.

Write everything with h()

function MyComponent(props) {
  return h(
    "div",
    { class: "my-component" },
    h(InnerComponent, { propA: "foo", propB: bar })
  );
}

Perfect for typing, but too verbose.

Write HTML with htm, but insert custom components with h()

function MyComponent(props) {
  return html`
    <div class="my-component">
      ${h(InnerComponent, { propA: "foo", propB: bar })}
    </div>
  `;
}

This looks like a good middle ground, but I'm not sure if it would work.

@pastelmind pastelmind added the type: enhancement New feature or request label Sep 7, 2020
@pastelmind pastelmind self-assigned this Sep 7, 2020
@pastelmind pastelmind changed the title Use h() instead of htm Use h() instead of htm for components Sep 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant