Skip to content

Commit

Permalink
Fix issue where root components were re-mounted instead of updated
Browse files Browse the repository at this point in the history
Previously, the ObservedComponent was recreated with every update. This fixes the issue by using the same component for the lifespan of the element.
  • Loading branch information
chasenlehara committed May 7, 2020
1 parent a36b515 commit d03ac2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/bitovi/react-to-can-webcomponent.git"
"url": "git+https://github.com/canjs/react-to-can-webcomponent.git"
},
"keywords": [
"React",
Expand All @@ -51,7 +51,7 @@
"author": "Bitovi",
"license": "MIT",
"bugs": {
"url": "https://github.com/bitovi/react-to-can-webcomponent/issues"
"url": "https://github.com/canjs/react-to-can-webcomponent/issues"
},
"homepage": "https://github.com/bitovi/react-to-can-webcomponent#readme"
"homepage": "https://github.com/canjs/react-to-can-webcomponent#readme"
}
11 changes: 11 additions & 0 deletions react-to-can-webcomponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ QUnit.module("react-to-can-webcomponent");


QUnit.test("basics with react", function(assert) {
var mountCount = 0;
var unmountCount = 0;

class Welcome extends React.Component {
componentDidMount() {
mountCount += 1;
}
componentWillUnmount() {
unmountCount += 1;
}
render() {
return <h1>Hello, {
this.props.name
Expand All @@ -42,6 +51,8 @@ QUnit.test("basics with react", function(assert) {
myWelcome.name = "Justin";

assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Justin", "can update");
assert.equal(mountCount, 1, "component has only been mounted once");
assert.equal(unmountCount, 0, "component has not been unmounted");

});

Expand Down
10 changes: 6 additions & 4 deletions react-to-can-webcomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default function(ReactComponent, React, ReactDOM) {
var targetPrototype = Object.create(HTMLElement.prototype);
targetPrototype.constructor = WebComponent;

var ObservedComponent = function(props) {
useObserver(React);
return React.createElement(ReactComponent, props);
};

// But have that prototype be wrapped in a proxy.
var proxyPrototype = new Proxy(targetPrototype, {
has: function (target, key) {
Expand Down Expand Up @@ -91,10 +96,7 @@ export default function(ReactComponent, React, ReactDOM) {
}, this);
rendering = true;
this[reactComponentSymbol] = ReactDOM.render(
React.createElement(props => {
useObserver(React);
return React.createElement(ReactComponent, props);
}, data),
React.createElement(ObservedComponent, data),
this
);
rendering = false;
Expand Down

0 comments on commit d03ac2f

Please sign in to comment.