Skip to content

Commit 115bad6

Browse files
committed
🚑 Improve code
1 parent 6f3abdf commit 115bad6

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/core.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ export default function r2wc<Props, Context>(
6363
mapAttributeProp[attribute] = prop;
6464
}
6565

66+
type PropsInContainer = Props & {
67+
container: HTMLElement;
68+
};
69+
6670
class ReactWebComponent extends HTMLElement {
6771
private connected = false;
6872
private context?: Context;
69-
props: Props = {} as Props;
73+
private _props = {} as PropsInContainer;
7074
container: HTMLElement;
7175
observer: MutationObserver;
7276

@@ -85,8 +89,20 @@ export default function r2wc<Props, Context>(
8589
this.container = this;
8690
}
8791

88-
// @ts-ignore: There won't always be a container in the definition
89-
this.props.container = this.container;
92+
this._props.container = this.container;
93+
}
94+
95+
public set props(newValue: Props) {
96+
this._props = {
97+
...this._props,
98+
...newValue,
99+
};
100+
101+
this.update();
102+
}
103+
104+
public get props(): Props {
105+
return this._props;
90106
}
91107

92108
connectedCallback() {
@@ -102,6 +118,7 @@ export default function r2wc<Props, Context>(
102118
disconnectedCallback() {
103119
this.connected = false;
104120

121+
this.observer.disconnect();
105122
this.unmount();
106123
}
107124

@@ -126,10 +143,10 @@ export default function r2wc<Props, Context>(
126143
const type = propTypes[prop];
127144
const transform = transforms[type];
128145
if (prop in reactProps) {
129-
this.props[prop] = value;
146+
this._props[prop] = value;
130147
} else if (value && transform?.parse) {
131148
//@ts-ignore
132-
this.props[prop] = transform.parse(value, this);
149+
this._props[prop] = transform.parse(value, this);
133150
}
134151
}
135152

0 commit comments

Comments
 (0)