Skip to content

Commit

Permalink
🐛 Prevent rendering if component is not connected to dom
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoLegends committed Jan 15, 2018
1 parent c880b61 commit 6a2fbcb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fit-html",
"version": "0.4.1",
"version": "0.4.2",
"description": "5KB functional Web Components without bloat",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default function connect<S, SP, DP, OP = {}>(
templateFn: (props: SP & DP) => TemplateResult,
): ClassConstructor<FitElement<S, SP & DP, OP>> {
return class extends HTMLElement {
private _isConnected: boolean = false;
private _preparedDispatch: MapDispatchToPropsFn<S, DP, OP> | ActionCreatorsMapObject;
private _preparedMapStateToProps: MapStateToPropsFn<S, SP, OP>;
private _previousProps: SP & DP | null = null;
Expand All @@ -135,6 +136,8 @@ export default function connect<S, SP, DP, OP = {}>(
}

connectedCallback() {
this._isConnected = true;

const store = this.getStore();
this._preparedDispatch = isFunction(mapDispatchToProps)
? mapDispatchToProps
Expand All @@ -145,6 +148,8 @@ export default function connect<S, SP, DP, OP = {}>(
}

disconnectedCallback() {
this._isConnected = false;

this._unsubscribe();
this._store = undefined!;
}
Expand Down Expand Up @@ -192,6 +197,10 @@ export default function connect<S, SP, DP, OP = {}>(
}

render() {
if (!this._isConnected) {
return;
}

const props = this.getProps();

if (shallowEqual(props, this._previousProps)) {
Expand Down

0 comments on commit 6a2fbcb

Please sign in to comment.