Skip to content

Commit 1c778cf

Browse files
committed
Fix #23 - Render canjs component only once on initial load
Override can-view-callbacks renderComponent function to when element auto-mount is false the component isn't auto mounted
1 parent 3169d61 commit 1c778cf

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

can-react-component.js

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var namespace = require("can-namespace");
33
var assign = require("can-assign");
44
var reflect = require("can-reflect");
55
var canSymbol = require("can-symbol");
6+
var viewCallbacks = require("can-view-callbacks");
67
var viewModelSymbol = canSymbol.for("can.viewModel");
78

89
module.exports = namespace.reactComponent = function canReactComponent(displayName, CanComponent) {
@@ -11,6 +12,13 @@ module.exports = namespace.reactComponent = function canReactComponent(displayNa
1112
displayName = (CanComponent.shortName || "CanComponent") + "Wrapper";
1213
}
1314

15+
const __renderComponent = viewCallbacks._tags[CanComponent.prototype.tag];
16+
viewCallbacks._tags[CanComponent.prototype.tag]= function renderComponent(el){
17+
if(el.getAttribute("auto-mount") !== "false"){
18+
__renderComponent.apply(this, arguments);
19+
}
20+
};
21+
1422
function Wrapper() {
1523
React.Component.call(this);
1624

@@ -47,6 +55,7 @@ module.exports = namespace.reactComponent = function canReactComponent(displayNa
4755
render: function() { // eslint-disable-line react/display-name
4856
return React.createElement(CanComponent.prototype.tag, {
4957
ref: this.createComponent,
58+
"auto-mount": "false"
5059
});
5160
}
5261
});

test/test.js

+38
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,42 @@ QUnit.module('can-react-component', (moduleHooks) => {
143143

144144
});
145145

146+
147+
QUnit.test('should be rendered only once', (assert) => {
148+
assert.expect(3);
149+
const ConsumedComponent = canReactComponent(
150+
'ConsumedComponent',
151+
CanComponent.extend('ConsumedComponent', {
152+
tag: "consumed-component4",
153+
ViewModel: {
154+
first: {
155+
type: 'string',
156+
default: 'Ivo'
157+
},
158+
last: 'string',
159+
name: {
160+
get() {
161+
return this.first + ' ' + this.last;
162+
}
163+
},
164+
connectedCallback(el){
165+
if(el.getAttribute("auto-mount") === "false") {
166+
assert.equal(this.last, "Pinheiro", `'last' name should be 'Pinheiro'`);
167+
}
168+
}
169+
},
170+
view: stache("<div class='inner'>{{name}}</div>")
171+
})
172+
);
173+
174+
container.innerHTML="<consumed-component4></consumed-component4>";
175+
176+
let divComponent = document.querySelector('consumed-component4');
177+
assert.equal(getTextFromFrag(divComponent), 'Ivo undefined');
178+
179+
ReactDOM.render(<ConsumedComponent last={"Pinheiro"} />, container);
180+
divComponent = document.querySelector('consumed-component4');
181+
assert.equal(getTextFromFrag(divComponent), 'Ivo Pinheiro');
182+
});
183+
146184
});

0 commit comments

Comments
 (0)