Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use function for composition #82

Closed
rbiggs opened this issue Feb 12, 2017 · 7 comments
Closed

How to use function for composition #82

rbiggs opened this issue Feb 12, 2017 · 7 comments

Comments

@rbiggs
Copy link
Contributor

rbiggs commented Feb 12, 2017

In the checkin #77 you mention about returning a function for composition. This is not mentioned in the README. An example would be nice to illustrate how this works. Is this a feature that only works with JSX? Is so, could it also be implemented to work with Hyperx?

@tunnckoCore
Copy link

tunnckoCore commented Feb 12, 2017

Nope, it isn't only JSX.

You could do

const { h } = require('hyperapp')

function Foo ({ name, title, children }) {
  return h('section', [
    h('h1.heading', title + ' ' + name),
    h('p', children)
  ])
}
const tree = h(Foo, { name: 'Charlike', title: 'Hello' }, 'Hello World')

@tunnckoCore
Copy link

tunnckoCore commented Feb 12, 2017

and JSX variant, i believe

/** @jsx h */
const { h } = require('hyperapp')

const Foo = ({ name, title, children }) => (<section>
  <h1 className="heading">{title} {name}</h1>
  <p children={children} />
</section>)

const tree = <Foo name="Charlike" title="Hello">Hello World</Foo>

That <p /> may look kinda strange, but I believe it would work. Another way is as usual

<p>{children}</p>

Because children is just a Node. Maybe. For that virtual dom i'm not sure if all that will work. But in mich-h it works that way and looks fantastic, kinda love it :)

@jorgebucaran
Copy link
Owner

jorgebucaran commented Feb 12, 2017

@rbiggs This feature only works for JSX. I'll update the docs to add an example.

In Hyperx you can do like @ungoldman's suggests in choojs/hyperx#9), but that's not nearly as convenient as <MyComponent propts=...>children</MyComponent>.

@niksy's fork of hyperxify seems to deal with this via Hyperx, but it hasn't been merged yet.

Our fork of hyperxify to support custom h functions hasn't been merged either:

Maybe @substack's is busy? 👋

If we can get that merged, we might be able to propose further changes, like <MyComponent></MyComponent>-style via Hyperx, which would be sweet.

@tunnckoCore
Copy link

tunnckoCore commented Feb 12, 2017

@jbucaran This feature only works for JSX.

Why not to work for raw h calls? It would, since h can accept first argument to be function as what tags with uppercased first later is transformed/transpiled to. Nothing needed.

const tree = <Foo name="Charlike" title="Hello">Hello World</Foo>
// => h(Foo, { name: 'Charlike', title: 'Hello' }, 'Hello World')

if it is <foo ... instead of <Foo it will be h('foo' ...) instead of h(Foo ...)

edit: just to be clear if some ELI5 comes haha :) I just learned it yesterday, it got me few hours to understand what happens when is uppercased first letter and why my virtual dom fails, haha.

  • <foo name="hello">world</foo> => h('foo', { name: 'hello' }, 'world')
  • <Foo name="hello">world</Foo> => h(Foo, { name: 'hello' }, 'world')

@jorgebucaran
Copy link
Owner

jorgebucaran commented Feb 12, 2017

@tunnckoCore Right. I overlooked it also works with raw h calls.

This is evident from the fact JSX is translated into h calls when using babel.

@tunnckoCore
Copy link

Yea just seen the slider. It looks great!

when using babel.

Or rollup-plugin-jsx

@jorgebucaran
Copy link
Owner

jorgebucaran commented Feb 17, 2017

@rbiggs I think we can close now. Function composition is supported in HyperApp via h. As a result, it just works with JSX, but not with Hyperx, unfortunately, and there's nothing actionable we can do here.

It doesn't look like it will be supported any time soon, see:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants