Skip to content

Commit 9eb8e6d

Browse files
Merge pull request #7 from ReactBangalore/exercise/performance-optimization
Updated UI for performance optimization exercise.
2 parents 003aa1b + f6a3818 commit 9eb8e6d

File tree

7 files changed

+139
-22
lines changed

7 files changed

+139
-22
lines changed

exercises/src/index.css

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,58 @@ body {
2626
flex: 3;
2727
flex-direction: column;
2828
}
29+
30+
.Counter {
31+
background-color: rgb(255, 255, 255);
32+
list-style: none;
33+
-webkit-padding-start: 0px;
34+
}
35+
.Counter__button {
36+
display: inline-block;
37+
-webkit-appearance: button;
38+
user-select: none;
39+
outline: 0px;
40+
border-width: 0px;
41+
border-style: initial;
42+
border-color: initial;
43+
border-image: initial;
44+
border-radius: 4px;
45+
position: relative;
46+
-webkit-font-smoothing: antialiased;
47+
width: 100%;
48+
font-size: 18px;
49+
padding: 14px 12px;
50+
background-color: rgb(246, 104, 38);
51+
color: white;
52+
font-weight: 600;
53+
}
54+
55+
.Counter__button:active {
56+
opacity: 0.8;
57+
}
58+
59+
.Counter__item {
60+
height: 56px;
61+
display: flex;
62+
-webkit-box-align: center;
63+
align-items: center;
64+
-webkit-box-pack: justify;
65+
justify-content: center;
66+
position: relative;
67+
border-bottom: 1px solid rgb(234, 234, 234);
68+
}
69+
70+
.Counter__actions {
71+
display: flex;
72+
-webkit-box-align: center;
73+
align-items: center;
74+
}
75+
76+
.Counter__count {
77+
width: 50px;
78+
text-align: center;
79+
}
80+
81+
.Counter__disabled {
82+
opacity: 0.3;
83+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { Component } from 'react'
2+
import PropTypes from 'prop-types'
3+
4+
import subtractIcon from "./icons/subtract.svg";
5+
import addIcon from "./icons/add.svg";
6+
7+
export default class Count extends Component {
8+
static propTypes = {
9+
onIncrement: PropTypes.func,
10+
onDecrement: PropTypes.func,
11+
value: PropTypes.number,
12+
}
13+
constructor(props) {
14+
super(props);
15+
this.getClassNamesForButton(props);
16+
}
17+
getClassNamesForButton = (props) => {
18+
const { value } = props;
19+
this.addClassName = value >= 10 ? "Counter__disabled" : "";
20+
this.subtractClassName = value <= 0 ? "Counter__disabled" : "";
21+
}
22+
componentWillReceiveProps(nextProps) {
23+
this.getClassNamesForButton(nextProps);
24+
}
25+
render() {
26+
const { onDecrement, value, onIncrement } = this.props;
27+
return (
28+
<ul className="Counter">
29+
<li className="Counter__item">
30+
<div className="Counter__actions">
31+
<span onClick={ onDecrement } className={ this.subtractClassName }>
32+
<img src={ subtractIcon } />
33+
</span>
34+
<p className="Counter__count">{ value }</p>
35+
<span onClick={ onIncrement } className={ this.addClassName }>
36+
<img src={ addIcon } />
37+
</span>
38+
</div>
39+
</li>
40+
</ul>
41+
);
42+
}
43+
}

exercises/src/performance-optimizations/Counter.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React, { Component, Fragment } from "react";
2+
import Count from "./Count";
23
import superExpensiveFunction from "./utils/superExpensiveFunction";
34

45
class Counter extends Component {
5-
static buttonStyle = {
6-
padding: "5px"
7-
};
86
state = {
97
count: 0,
108
timestamp: Date.now()
@@ -14,8 +12,20 @@ class Counter extends Component {
1412
count: prevState.count + 1
1513
});
1614

17-
handleCounterClick = () => {
18-
this.setState(this.increment);
15+
decrement = prevState => ({
16+
count: prevState.count - 1
17+
});
18+
19+
handleIncrementClick = () => {
20+
if (this.state.count < 10) {
21+
this.setState(this.increment);
22+
}
23+
};
24+
25+
handleDecrementClick = () => {
26+
if(this.state.count > 0) {
27+
this.setState(this.decrement);
28+
}
1929
};
2030

2131
handleDummyOperationClick = () => {
@@ -24,22 +34,21 @@ class Counter extends Component {
2434
});
2535
};
2636

27-
shouldComponentUpdate(nextProps, nextState) {
28-
// Implement shouldComponentUpdate to avoid unnecessary re-renders.
29-
}
37+
// Implement shouldComponentUpdate to avoid unnecessary re-renders.
38+
// shouldComponentUpdate(nextProps, nextState) {
39+
// }
3040

3141
render() {
3242
const { count } = this.state;
3343
const calculatedCount = superExpensiveFunction(count);
3444
return (
3545
<Fragment>
36-
<div>Clicks: {calculatedCount}</div>
37-
<div style={Counter.buttonStyle}>
38-
<button onClick={this.handleCounterClick}>Increment</button>
39-
</div>
40-
<div style={Counter.buttonStyle}>
41-
<button onClick={this.handleDummyOperationClick}>Dummy</button>
42-
</div>
46+
<Count
47+
onIncrement={ this.handleIncrementClick }
48+
onDecrement={ this.handleDecrementClick }
49+
value={ calculatedCount }
50+
/>
51+
<button className="Counter__button" onClick={this.handleDummyOperationClick}>Dummy</button>
4352
</Fragment>
4453
);
4554
}

exercises/src/performance-optimizations/Hello.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Loading
Lines changed: 6 additions & 0 deletions
Loading

exercises/src/performance-optimizations/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import Counter from "./Counter";
33

44
const styles = {
55
fontFamily: "sans-serif",
6-
textAlign: "center"
6+
textAlign: "center",
7+
padding: "40px 120px 0"
78
};
89

9-
const App = () => (
10+
const PerformanceOptimizationExample = () => (
1011
<div style={styles}>
11-
<p>Implement shouldComponentUpdate in Counter to avoid unnecessary re-renders.</p>
12+
<b>Implement shouldComponentUpdate in Counter & Count components to avoid unnecessary re-renders.</b>
1213
<Counter />
1314
</div>
1415
);
1516

16-
export default App
17+
export default PerformanceOptimizationExample;

0 commit comments

Comments
 (0)