-
Notifications
You must be signed in to change notification settings - Fork 17
/
react.js
43 lines (33 loc) · 1.17 KB
/
react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var assert = require('assert')
module.exports = toReact
function toReact (Component, react) {
assert.equal(typeof Component, 'function', 'nanocomponent-adapters/react: component should be type function')
assert.equal(typeof react, 'object', 'nanocomponent-adapters/react: react should be type object')
function Clx () {
react.Component.apply(this, arguments)
this.comp = new Component()
this.node = null
this.setRef = this.setRef.bind(this)
}
Clx.prototype = Object.create(react.Component.prototype)
Clx.prototype.constructor = Clx
Clx.prototype.componentDidMount = function componentDidMount () {
if (!this.comp.element) {
var el = this.comp.render(this.props)
this.node.appendChild(el)
}
}
Clx.prototype.setRef = function (_node) {
this.node = _node
}
Clx.prototype.componentWillReceiveProps = function componentWillReceiveProps (props) {
if (this.comp.element) this.comp.render(props)
}
Clx.prototype.shouldComponentUpdate = function shouldComponentUpdate () {
return false
}
Clx.prototype.render = function render (props) {
return react.createElement('div', { ref: this.setRef })
}
return Clx
}