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

Ability to pass just a single object #65

Open
balupton opened this issue Mar 29, 2017 · 9 comments
Open

Ability to pass just a single object #65

balupton opened this issue Mar 29, 2017 · 9 comments

Comments

@balupton
Copy link
Member

balupton commented Mar 29, 2017

It could be nice if we could just pass one object, that contains a tag and children properties, like so:

h({
	tag: 'a',
	class: 'prev',
	href: prev.url,
	children: [
		h('span.icon'),
		h('span.title', prev.title)
	]
})

I'm doing a gist now to test this assumption.

@reqshark
Copy link
Member

reqshark commented Mar 29, 2017

i agree, both ways are good, ought to be supported:
• array children as an arg or
• more declarative children props

@balupton
Copy link
Member Author

so I did up a gist over at: https://gist.github.com/balupton/c807a9be215723f0e849697097b99a50

I'm not actually sure this suggestion results in better code, it sure makes understanding the hierarchy easier, but it does double the lines of code

@reqshark
Copy link
Member

that gist makes things look more readable using the children style in my opinion

@dominictarr
Copy link
Collaborator

dominictarr commented Mar 29, 2017

I could tolerate (what this will do to the implementation internals) if this only applies when the object is the first arg (if the tag name is not the string) and if all fields are under properties.

h({tag: ..., classes: ..., attributes: {...}, events: {...}, children: [...] })

with only a fixed number of accepted properties on the top object. then the code for handling any of those things can be refactored into a function and just called on the object.

I disagree that it's more "readable" but that is very subjective.

@reqshark
Copy link
Member

yes of course, so it may not be more "readable"

but there is something less subjective here if we believe in the following premise:

  • a function that accepts fewer arguments is simpler

@reqshark
Copy link
Member

also if we do tag and children, let's also do a textNode prop like

h({tag: ..., classes: ..., attributes: {...}, text: ... })

@dominictarr
Copy link
Collaborator

@ahdinosaur I am looking for the issue that you opened regards consolidating the hyperscript api, but can't find it.

@ahdinosaur
Copy link

@dominictarr thanks for the ping: #66

@dmitriz
Copy link

dmitriz commented Apr 23, 2017

Passing objects is also easier from the Functional Programming perspective, if you want to compose h with some pure functions:

var link = {tag: 'a', href: 'http://check.it', text: 'Check my link!'}
var applyStyle = style => elm => {...elm, style}
var styledNode = h(applyStyle(link)) 
  //=> h({tag: 'a', href: 'http://check.it', style: style, text: 'Check my link!'})

would be easier than

var textInput = {tag: 'input, href: 'http://check.it', text:'Check my link!'}
var applyStyle = style => elm => {...elm, style}
var removeProps = props => obj => {
   ... // write function removing the props from the object
}
var toArray = obj => [obj.tag, obj, obj.text]
var styledNode = h.apply(h, toArray(removeProps(['tag', 'text'])(applyStyle(link))))
  //=> h('a', {href: 'http://check.it', style: style}, 'Check my link!')

that you need now with the current implementation of h.

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

5 participants