Skip to content

Commit

Permalink
Merge pull request #11 from psychobolt/rerender-unmount-support
Browse files Browse the repository at this point in the history
Support child component re-render and unmounting
  • Loading branch information
sasha240100 committed Mar 26, 2018
2 parents 8e21029 + 05b39da commit b76d7c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/TransitionBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import React, {Component} from 'react';

export class TransitionBase extends Component {
defer = [];
mounted = false;

deferParent(func) {
this.defer.push(func);
}

wrapChild(child) {
return React.cloneElement(child, {
parent: this.native,
parent: this,
onParentMount: this.deferParent.bind(this)
});
}
Expand All @@ -23,9 +24,20 @@ export class TransitionBase extends Component {
}

mount() {
if (this.props.onParentMount && this.props.parent)
this.props.onParentMount(() => this.props.parent.add(this.native));
if (this.props.parent)
if (this.props.parent.mounted)
this.props.parent.native.add(this.native);
else if (this.props.onParentMount)
this.props.onParentMount(() => this.props.parent.native.add(this.native));

this.defer.forEach(func => func());
this.mounted = true;
}

unmount() {
if (this.props.parent)
this.props.parent.native.remove(this.native);

this.mounted = false;
}
}
4 changes: 4 additions & 0 deletions src/reactify.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export function reactify(component) {
componentDidMount() {
this.mount();
}

componentWillUnmount() {
this.unmount();
}
}
}

0 comments on commit b76d7c6

Please sign in to comment.