-
Notifications
You must be signed in to change notification settings - Fork 49
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
Using JSX with Bel #56
Comments
Sometimes I have wished that all the |
What does the compiled JSX code look like? |
Compiled JSX with const el = createElement(
'h1',
null,
'Hi!'
) And Bel, according to the docs, expects it to be: var sameElement = createElement('h1', { className: 'heading' }, ['Hi!']) More complex nested example: const el = <div>
<h1>Hi, <strong>stranger</strong></h1>
<h2>Hello</h2>
</div> Transpiled: const el = createElement(
'div',
null,
createElement(
'h1',
null,
'Hi, ',
createElement(
'strong',
null,
'stranger'
)
),
createElement(
'h2',
null,
'Hello'
)
); |
So their spec expects |
So you’d want to support both or switch to their? |
Support both if possible, not switch to theirs. |
Hmmm, I feel the solution for bel / react compat would probably be to create Does that make sense? On Sun, Oct 23, 2016 at 5:25 AM nichoth [email protected] wrote:
|
seems it took a while for my emails to arrive 😅 On Tue, Oct 25, 2016 at 8:42 PM Yoshua Wuyts [email protected]
|
One more thing related to this "spec" compliance is because to support components, not tags which means passing a functions as first argument not a string var el = <Link foo="bar">Hello</Link> is transpiled to // should also support content to be a string, not an array
// because currently isn't possible
var el = createElement(Link, { foo: 'bar' }, ['Hello']) |
kinda such thing function arrayify (val) {
if (!val) return [];
if (Array.isArray(val)) return val;
return [val];
}
function isObject (val) {
return val && typeof val === 'object' && !Array.isArray(val)
}
function belCreateElement (tag, props, children) {
var el
children = arrayify(children)
props = isObject(props) ? props : {}
if (typeof tag === 'function') {
// not sure for the order
// but we should have `children` on `props` object
props.children = children || arrayify(props.children)
var _ele = tag(props, props.children)
// should remove `_ele._attributes.null.children`
return _ele
}
// not changed code below ...
} seems to be working examples function Link (props) {
return belCreateElement('a', props, props.children.concat(' world!!'))
}
var el = belCreateElement(Link, { className: 'foo' }, 'Hello')
console.log(el.toString()) Ahref, not using function Ahref ({ className, children }) {
return belCreateElement('a', { className }, children.concat(' world!!'))
}
var el = belCreateElement(Ahref, { className: 'foo' }, 'Hello')
console.log(el.toString()) MOAR var hx = require('hyperx')(belCreateElement)
function Link (props) {
// JSX
// <a { ...props }>{ props.children.concat(' world...').join('') }</a>
return hx`<a ${ /* ...props */ } >${ props.children.concat(' world...').join('') }</a>`
}
// JSX
// var el = <Link className="foobar">Hello</Link>
var el = belCreateElement(Link, { className: 'foobar' }, 'Hello')
console.log(el.toString())
// expected
// => '<a class="foobar">Hello world...</a>' |
I was wondering if its possible to use JSX instead of hyperx to create elements with Bel. I like to use Bel and Yo-yo, but sometimes its convenient to go JSX route, because then components could be re-used between Yo-yo/Choo and React projects.
I tried to set
jsx pragma
toh
and then use it like so:But children (
Hi
) never appear, and if you omitclass
, bel complaints:Cannot read property 'namespace' of null
. Got confused, probably missing something.The text was updated successfully, but these errors were encountered: