Skip to content
This repository was archived by the owner on Feb 27, 2019. It is now read-only.

Commit b52b2dd

Browse files
committed
Add onMount and onUnmount to reactive components
1 parent 761a370 commit b52b2dd

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ const Example = ({date}) => {
8080
}
8181
```
8282

83+
### Mounting / unmounting hooks
84+
85+
Because `ref` can't be used with React class components, reactive components
86+
signal their mounting and un-mounting with `onMount` and `onUnmount` lifecycle
87+
hooks.
88+
8389
### Creating observables from DOM events
8490

8591
#### Emitting events

src/fromClass.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,23 @@ export default Class => React.createClass({
5555
},
5656

5757
render() {
58+
const {props} = this.state
59+
return props ? this.renderInner() : null
60+
},
61+
62+
renderInner() {
5863
const {props, eventSink} = this.state
59-
return props ? React.createElement(Class, bindEventSink(Class, eventSink, props), props.children) : null
64+
const newProps = bindEventSink(Class, eventSink, props)
65+
if (newProps.onMount || newProps.onUnmount) {
66+
newProps.ref = node => {
67+
if (node === null && newProps.onUnmount) {
68+
newProps.onUnmount(node)
69+
} else if (node !== null && newProps.onMount) {
70+
newProps.onMount(node)
71+
}
72+
}
73+
}
74+
return React.createElement(Class, newProps, props.children)
6075
},
6176

6277
_onObsNext(obs) {

0 commit comments

Comments
 (0)