Skip to content

Commit e101043

Browse files
committed
update re-render method
1 parent 92526e1 commit e101043

3 files changed

Lines changed: 45 additions & 30 deletions

File tree

src/components/Page.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const AUTO_RENDER_COUNT = 5;
66

77
class Page extends React.Component {
88
state = {
9+
action: null,
910
type: null,
1011
auto: false,
1112
counter: 0,
@@ -21,6 +22,9 @@ class Page extends React.Component {
2122
input = null;
2223
checkbox = null;
2324

25+
clearButton = null;
26+
renderButton = null;
27+
2428
componentDidMount() {
2529
this.props.load(({ block, differentBlocks }) => {
2630
this.setState({ block, differentBlocks });
@@ -29,42 +33,51 @@ class Page extends React.Component {
2933

3034
onRenderSameComponents = () => {
3135
this.setState({
36+
action: 'render',
3237
type: 'SAME_COMOPNENT',
33-
auto: false,
38+
counter: this.state.auto ? ++this.state.counter : this.state.counter,
3439
});
3540
};
3641

3742
onRenderDifferentComponents = () => {
3843
this.setState({
44+
action: 'render',
3945
type: 'DIFFERENT_COMOPNENTS',
40-
auto: false,
4146
});
4247
};
4348

4449
onAutoRender = () => {
4550
this.setState({
46-
type: 'SAME_COMOPNENT',
51+
action: 'autoRender',
52+
// type: 'SAME_COMOPNENT',
4753
auto: true,
48-
counter: ++this.state.counter,
4954
});
5055
};
5156

5257
onClear = () => {
5358
this.setState({
59+
action: 'clear',
5460
type: null,
55-
auto: false,
5661
});
5762
};
5863

5964
componentDidUpdate() {
6065
if (this.state.auto) {
6166
if (this.state.counter !== AUTO_RENDER_COUNT) {
62-
setTimeout(() => {
63-
this.onClear();
67+
if (this.state.action === 'render') {
68+
// setTimeout to show visual re-render
6469
setTimeout(() => {
65-
this.onAutoRender();
66-
});
67-
});
70+
this.clearButton.click();
71+
}, 500);
72+
} else if (
73+
this.state.action === 'clear' ||
74+
this.state.action === 'autoRender'
75+
) {
76+
// setTimeout to show visual re-render
77+
setTimeout(() => {
78+
this.renderButton.click();
79+
}, 500);
80+
}
6881
} else {
6982
this.setState({
7083
counter: 0,
@@ -117,8 +130,6 @@ class Page extends React.Component {
117130
);
118131
}
119132

120-
renderPerf() {}
121-
122133
renderPage() {
123134
if (!this.state.type) {
124135
return (
@@ -227,9 +238,8 @@ class Page extends React.Component {
227238
<ScrollView width="100%" flex={1}>
228239
<Box center className="controlls-space">
229240
<button
230-
disabled={
231-
!this.state.block || this.state.type || this.state.counter !== 0
232-
}
241+
ref={el => (this.renderButton = el)}
242+
disabled={!this.state.block || this.state.type}
233243
onClick={this.onRenderSameComponents}
234244
>
235245
Render 1000 same components
@@ -244,9 +254,7 @@ class Page extends React.Component {
244254
</a>
245255

246256
<button
247-
disabled={
248-
!this.state.block || this.state.type || this.state.counter !== 0
249-
}
257+
disabled={!this.state.block || this.state.type}
250258
onClick={this.onRenderDifferentComponents}
251259
>
252260
Render 1000 different components
@@ -261,16 +269,15 @@ class Page extends React.Component {
261269
</a>
262270

263271
<button
264-
disabled={
265-
!this.state.block || this.state.type || this.state.counter !== 0
266-
}
272+
disabled={!this.state.block || this.state.type}
267273
onClick={this.onAutoRender}
268274
>
269275
Render {AUTO_RENDER_COUNT} times
270276
</button>
271277

272278
<button
273-
disabled={!this.state.type || this.state.counter !== 0}
279+
ref={el => (this.clearButton = el)}
280+
disabled={!this.state.type}
274281
onClick={this.onClear}
275282
>
276283
Clear

src/components/Perfomance.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class Perfomance extends Component {
1919
firstDidMountTimeEl = null;
2020
firstRenderStylesTimeEl = null;
2121

22-
showFirstRender = false;
23-
2422
constructor(props) {
2523
super(props);
2624
this.createdAt = now();
@@ -73,6 +71,8 @@ class Perfomance extends Component {
7371
this.showFirstRender = true;
7472
}
7573

74+
console.log(this.showFirstRender);
75+
console.log(metrics);
7676
metrics = [];
7777
}
7878
});
@@ -100,9 +100,7 @@ class Perfomance extends Component {
100100

101101
return (
102102
<div>
103-
<div
104-
className={`${this.showFirstRender ? 'empty-time-container ' : ''}first-render time-container`}
105-
>
103+
<div className="first-render time-container">
106104
<div
107105
ref={el =>
108106
(this.firstDidMountTimeEl = el ? el.querySelector('span') : null)}
@@ -148,7 +146,9 @@ class Perfomance extends Component {
148146
</div>
149147
</div>
150148

151-
{components}
149+
<div className="components">
150+
{components}
151+
</div>
152152
</div>
153153
);
154154
}

src/index.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@ body {
4343
}
4444

4545
.empty-time-container {
46-
display: none;
46+
display: none !important;
47+
}
48+
49+
.time-container + .time-container {
50+
margin-top: 10px;
4751
}
4852

4953
.time-container {
5054
display: flex;
51-
height: 50px;
55+
/*height: 50px;*/
5256
justify-content: center;
5357
}
5458

59+
.components {
60+
margin-top: 10px;
61+
}
62+
5563
.time {
5664
font-size: 1.5em;
5765

0 commit comments

Comments
 (0)