Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StencilJS: Missing "h" import for JSX types

I am trying to create a web component using the stencil and while using the h1 tag inside TSX file I get below error:

[ ERROR ]  Missing "h" import for JSX types: ./src/components/side-drawer/side-drawer.tsx:1:19
           In order to load accurate JSX types for components, the "h" function must be imported from "@stencil/core"
           by each component using JSX. For example: import { Component, h } from '@stencil/core';

Below is the code for my web component:

import { Component } from "@stencil/core";

@Component({
  tag: "vs-test",
})
export class Test {
  render() {
    return (
      <div>
        <h1>Hello</h1>
      </div>
    );
  }
}

like image 456
Varun Sukheja Avatar asked Jun 27 '26 05:06

Varun Sukheja


1 Answers

Okay so finally got it from the official docs.

There is a breaking change since v1.0.0 of stencil

BREAKING CHANGES

A common issue with JSX is each separate project's use of global JSX types. Many of the required changes are in order to avoid global types, which often cause issues for apps which import from numerous packages. The other change is having each component import its renderer, such as JSX's h() function.

Import { h } is required In order to render JSX in Stencil apps, the h() function must be imported from @stencil/core:

import { h } from '@stencil/core';

function app() {
  return <ion-app></ion-app>
}

The h stands for "hyperscript", which is what JSX elements are transformed into (it's the actual function executed when rendering within the runtime). Stencil's h import is an equivalent to React's React.createElement. This also explains why the app's tsconfig.json sets the { "jsxFactory": "h" } config, which is detailed further in TypeScript's JSX Factory Function Docs.

like image 128
Varun Sukheja Avatar answered Jun 29 '26 17:06

Varun Sukheja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!