In this example react-hyperscript
is curried and exposes a set of default functions, so h('div', props, children)
becomes div(props, children)
.
import hyperscript from 'react-hyperscript';
import {curry} from 'lodash';
const isString = v => typeof v === 'string' && v.length > 0;
const isSelector = v => isString(v) && (v[0] === '.' || v[0] === '#');
const h = curry(
(tagName, first, ...rest) =>
isString(tagName) && isSelector(first) ?
hyperscript(tagName + first, ...rest) :
hyperscript(tagName, first, ...rest)
);
const TAG_NAMES = [
'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', // ...
];
TAG_NAMES.forEach(tagName =>
Object.defineProperty(h, tagName, {
value: h(tagName),
writable: false,
})
);
export default h;
In another module:
import h, {div} from 'lib/h';
console.log(
h, // h
div, // undefined <- problem!
h('div'), // div
h.div // div
)
This can be resolved by appending this to the example (zip from lodash):
const {
a, abbr, address, area, // ...
} = zip(
TAG_NAMES,
TAG_NAMES.map(h)
)
export {
a, abbr, address, area, // ...
}
But this solution isn't very elegant, does anybody know a better alternative?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…