yixiang
2019-08-14 17:16:06 +08:00
是时候贴个原生 js 写 react 的代码了?不嫌 render 函数用 hyperscript 写着蛋疼的话,也没啥大问题。
```
var e = React.createElement;
var Comp = function (props) {
React.Component.call(this, props);
this.state = {...};
};
Comp.prototype = Object.create(React.Component.prototype);
// 方法
Comp.prototype.add = function () {};
// render
Comp.prototype.render = function () {
return [
e('input', { type: 'hidden', name: 'something', value: this.state.something, key: -2 }),
e('div', { className: 'field', key: -1 },
e('i', { className: 'icon plus link float-right' , onClick: this.add.bind(this) }),
e('label', null, '添加')
),
this.state.sentences.map(function (item, i) {
return e('div', ...);
}.bind(this)),
];
};
```