Skip to content

Commit

Permalink
Adds unmountComponentAtNode top level api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revanth Mahesh committed Jul 17, 2019
1 parent dc7383c commit 1a521b6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions example/UnMountAtNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Brahmos, { Component, unmountComponentAtNode } from '../src';

export default class UnMountAtNode extends Component {

removeNode = () => {
setTimeout(() => {
unmountComponentAtNode( document.getElementById( 'unmount-node' ) );
}, 1000);
}
componentWillUnmount(){
console.log( 'component will unmount life cycle called!!' );
}
render () {
return (
<div>
<p> Remove a mounted Brahmos component from the DOM and clean up its event handlers and state. If no component was mounted in the container, calling this function does nothing. Returns true if a component was unmounted and false if there was no component to unmount. </p>
<div>
<button onClick={ this.removeNode }> Remove node</button>
</div>
</div>
);
}
}
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<h1>Brahmos App</h1>
<div id="app"></div>
<div id="another-root"></div>
<div id="unmount-node"></div>

<script src="./index.js">
</script>
Expand Down
3 changes: 3 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import App from './App.js';
import UnMountAtNode from './UnMountAtNode';
import Brahmos, { render } from '../src';

render(<App />, document.getElementById('app'));

render( <UnMountAtNode/>, document.getElementById('unmount-node'))
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import createContext from './createContext';
/** ForwardRef and createRef */
import { forwardRef, createRef } from './refs';

/** unmountComponentAtNode */
import unmountComponentAtNode from './unmountComponentAtNode';

const Brahmos = {
createElement,
render,
Expand All @@ -45,6 +48,7 @@ const Brahmos = {
forwardRef,
createRef,
createPortal,
unmountComponentAtNode,
};

export {
Expand All @@ -65,6 +69,7 @@ export {
forwardRef,
createRef,
createPortal,
unmountComponentAtNode,
};

export default Brahmos;
14 changes: 14 additions & 0 deletions src/unmountComponentAtNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tearDown from './tearDown';

function unmountComponentAtNode( container ){
/**
* if container has a brahmosNode, it will be tear down.
*/
if( container.__brahmosNode ){
tearDown( container.__brahmosNode, { parentNode: container } );
return true;
}
return false;
}

export default unmountComponentAtNode;

0 comments on commit 1a521b6

Please sign in to comment.