Skip to content

Commit d03ac2f

Browse files
committed
Fix issue where root components were re-mounted instead of updated
Previously, the ObservedComponent was recreated with every update. This fixes the issue by using the same component for the lifespan of the element.
1 parent a36b515 commit d03ac2f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"repository": {
4343
"type": "git",
44-
"url": "git+https://github.com/bitovi/react-to-can-webcomponent.git"
44+
"url": "git+https://github.com/canjs/react-to-can-webcomponent.git"
4545
},
4646
"keywords": [
4747
"React",
@@ -51,7 +51,7 @@
5151
"author": "Bitovi",
5252
"license": "MIT",
5353
"bugs": {
54-
"url": "https://github.com/bitovi/react-to-can-webcomponent/issues"
54+
"url": "https://github.com/canjs/react-to-can-webcomponent/issues"
5555
},
56-
"homepage": "https://github.com/bitovi/react-to-can-webcomponent#readme"
56+
"homepage": "https://github.com/canjs/react-to-can-webcomponent#readme"
5757
}

react-to-can-webcomponent-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ QUnit.module("react-to-can-webcomponent");
1616

1717

1818
QUnit.test("basics with react", function(assert) {
19+
var mountCount = 0;
20+
var unmountCount = 0;
21+
1922
class Welcome extends React.Component {
23+
componentDidMount() {
24+
mountCount += 1;
25+
}
26+
componentWillUnmount() {
27+
unmountCount += 1;
28+
}
2029
render() {
2130
return <h1>Hello, {
2231
this.props.name
@@ -42,6 +51,8 @@ QUnit.test("basics with react", function(assert) {
4251
myWelcome.name = "Justin";
4352

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

4657
});
4758

react-to-can-webcomponent.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export default function(ReactComponent, React, ReactDOM) {
3737
var targetPrototype = Object.create(HTMLElement.prototype);
3838
targetPrototype.constructor = WebComponent;
3939

40+
var ObservedComponent = function(props) {
41+
useObserver(React);
42+
return React.createElement(ReactComponent, props);
43+
};
44+
4045
// But have that prototype be wrapped in a proxy.
4146
var proxyPrototype = new Proxy(targetPrototype, {
4247
has: function (target, key) {
@@ -91,10 +96,7 @@ export default function(ReactComponent, React, ReactDOM) {
9196
}, this);
9297
rendering = true;
9398
this[reactComponentSymbol] = ReactDOM.render(
94-
React.createElement(props => {
95-
useObserver(React);
96-
return React.createElement(ReactComponent, props);
97-
}, data),
99+
React.createElement(ObservedComponent, data),
98100
this
99101
);
100102
rendering = false;

0 commit comments

Comments
 (0)