Preact: Export vnode class

Created on 5 Mar 2018  路  4Comments  路  Source: preactjs/preact

I have my own h()-like function that wraps preact's h() function but it would be nice if I could skip the middle man and just import vnode and return them from my function.

question

Most helpful comment

I think you can set this up in your .babelrc via transform-react-jsx plugin:

{
  "plugins": [
    ["transform-react-jsx", {
      "pragma": "h" // your own h-like function name
    }]
  ]
}

and then import your h-like function in class file

All 4 comments

I think you can set this up in your .babelrc via transform-react-jsx plugin:

{
  "plugins": [
    ["transform-react-jsx", {
      "pragma": "h" // your own h-like function name
    }]
  ]
}

and then import your h-like function in class file

Also - worth noting that h() does more than just instance VNode. It normalizes arguments in a way that is required by the diff.

@yaodingyd Thanks, but I'm not using jsx or babel.

@developit Could you elaborate on that?

It looks like VNode is just an empty class so this works just fine, in which case there's no need to import vnode. Thanks.

var vnode = {
    nodeName: 'div',
    children: ['test']
};
render(vnode, document.body);

@Qrokqt yep, VNode is an empty class and your code will work fine. Just be aware that when somebody else uses your code they may expect h to work like preact's h. That's what @developit meant.

For example when props has a children property wie move copy that over to the vnode's children property and delete it from props.

h("div", { children: "foo", bar: "baz" })

// Without normalization
const VNode = {
  nodeName: "div",
  attributes: { children: "foo", bar: "baz" },
  children: [],
}

// Compare that to with normalization by preact's built-in h function
const VNode = {
  nodeName: "div",
  attributes: { bar: "baz" },
  children: ["foo"],
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthewmueller picture matthewmueller  路  3Comments

mizchi picture mizchi  路  3Comments

kay-is picture kay-is  路  3Comments

skaraman picture skaraman  路  3Comments

kossnocorp picture kossnocorp  路  3Comments