Skip to content

Commit 5d1aea0

Browse files
committed
Improve mount performance
1 parent a9751d8 commit 5d1aea0

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

src/styled.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
2222
const dynamicStyle = getDynamicStyles(elementStyle)
2323
const staticTagName = generateTagName(tagName)
2424

25+
const availableDynamicTagNames = []
26+
2527
return class StyledElement extends PureComponent {
2628
static tagName: string = tagName
2729
static style: ComponentStyleType = elementStyle
@@ -34,8 +36,8 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
3436

3537
constructor(props: StyledElementPropsType) {
3638
super(props)
37-
if (!this.dynamicTagName) {
38-
this.dynamicTagName = generateTagName(tagName)
39+
if (!this.dynamicTagName && dynamicStyle) {
40+
this.dynamicTagName = availableDynamicTagNames.pop() || generateTagName(tagName)
3941
}
4042
}
4143

@@ -46,15 +48,13 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
4648
this.staticSheet.addRule(staticTagName, elementStyle)
4749
}
4850

49-
if (dynamicStyle && !this.dynamicSheet.getRule(this.dynamicTagName)) {
50-
this.dynamicSheet
51-
.detach()
52-
.addRule(this.dynamicTagName, dynamicStyle)
53-
this.dynamicSheet
54-
.update(this.dynamicTagName, this.props)
55-
.attach()
56-
.link()
51+
if (!dynamicStyle) return
52+
53+
if (!this.dynamicSheet.getRule(this.dynamicTagName)) {
54+
this.dynamicSheet.addRule(this.dynamicTagName, dynamicStyle)
5755
}
56+
57+
this.dynamicSheet.update(this.dynamicTagName, this.props)
5858
}
5959

6060
componentWillReceiveProps(nextProps: StyledElementPropsType) {
@@ -63,6 +63,10 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
6363
}
6464
}
6565

66+
componentWillUnmount() {
67+
availableDynamicTagNames.push(this.dynamicTagName)
68+
}
69+
6670
render() {
6771
if (!this.staticSheet) return null
6872

src/tests/__snapshots__/index.spec.jsx.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
exports[`test renders correctly App with default Styled 1`] = `
22
<div
3-
className="div-14-0-9">
3+
className="div-9-0-9">
44
<header
5-
className="header-15-0-10">
5+
className="header-10-0-10">
66
<h1
7-
className="h1-18-0-11">
7+
className="h1-13-0-11">
88
Title
99
</h1>
1010
</header>
1111
<section
12-
className="section-16-0-12">
12+
className="section-11-0-12">
1313
<button
14-
className="button-19-0-13 button-24-0-14">
14+
className="button-14-0-13 button-15-0-14">
1515
primitive test
1616
</button>
1717
<button
18-
className="button-19-0-13 button-25-0-15">
18+
className="button-14-0-13 button-16-0-15">
1919
dynamic primitive test
2020
</button>
2121
</section>
2222
<section
23-
className="section-17-0-16">
23+
className="section-12-0-16">
2424
Another section
2525
</section>
2626
</div>
@@ -39,11 +39,11 @@ exports[`test renders correctly App with default styled 1`] = `
3939
<section
4040
className="section-3-0-3">
4141
<button
42-
className="button-6-0-4 button-11-0-5">
42+
className="button-6-0-4 button-7-0-5">
4343
primitive test
4444
</button>
4545
<button
46-
className="button-6-0-4 button-12-0-6">
46+
className="button-6-0-4 button-8-0-6">
4747
dynamic primitive test
4848
</button>
4949
</section>
@@ -58,27 +58,27 @@ exports[`test renders correctly App with injectStyled 1`] = `
5858
<div
5959
className="root-0-17">
6060
<div
61-
className="div-27-0-19">
61+
className="div-17-0-19">
6262
<header
63-
className="header-28-0-20">
63+
className="header-18-0-20">
6464
<h1
65-
className="h1-31-0-21">
65+
className="h1-21-0-21">
6666
Title
6767
</h1>
6868
</header>
6969
<section
70-
className="section-29-0-22">
70+
className="section-19-0-22">
7171
<button
72-
className="button-32-0-23 button-37-0-24">
72+
className="button-22-0-23 button-23-0-24">
7373
primitive test
7474
</button>
7575
<button
76-
className="button-32-0-23 button-38-0-25">
76+
className="button-22-0-23 button-24-0-25">
7777
dynamic primitive test
7878
</button>
7979
</section>
8080
<section
81-
className="section-30-0-26">
81+
className="section-20-0-26">
8282
Another section
8383
</section>
8484
</div>

0 commit comments

Comments
 (0)