Skip to content

Commit 70bd1cb

Browse files
committed
refactor
1 parent 81bac8a commit 70bd1cb

File tree

2 files changed

+47
-184
lines changed

2 files changed

+47
-184
lines changed

Module01/components.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Class Component
2+
class Button extends React.Component {
3+
handleClick = () => {
4+
this.props.onClickFunction(this.props.incrementValue);
5+
};
6+
7+
render() {
8+
return(
9+
<button onClick={this.handleClick}>
10+
+{this.props.incrementValue}
11+
</button>
12+
);
13+
}
14+
}
15+
16+
// Functional Component
17+
const Result = (props) => {
18+
return (
19+
<div>{props.counter}</div>
20+
);
21+
};
22+
23+
// Parent Class Component
24+
class App extends React.Component {
25+
state = { counter: 0 };
26+
27+
incrementCounter = (incrementValue) => {
28+
this.setState( (prevState) => ({
29+
counter: prevState.counter + incrementValue
30+
}));
31+
}
32+
33+
render() {
34+
return (
35+
<div>
36+
<Button incrementValue={1} onClickFunction={this.incrementCounter} />
37+
<Button incrementValue={5} onClickFunction={this.incrementCounter} />
38+
<Button incrementValue={10} onClickFunction={this.incrementCounter} />
39+
<Button incrementValue={100} onClickFunction={this.incrementCounter} />
40+
<Result counter={this.state.counter}/>
41+
</div>
42+
)
43+
}
44+
}
45+
46+
// React only renders one Component which is the reason for using a parent Component
47+
ReactDOM.render(<App />, mountNode);

index.html

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)