This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy path01 - Classes.js
47 lines (40 loc) · 1.58 KB
/
01 - Classes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// The only reason we need a React depencency here is because the base class
// provides the this.setState method.
import { Component } from "react";
// We can inline a named export call.
export class Button extends Component {
// Prop types are defined using built-in language support using a TypeScript
// compatible syntax. Notice the subtle syntax difference between the colon
// and the equal sign.
props : {
width: number
}
// Default properties can be defined as a static property initializer.
// The value of each property is shallow copied onto the final props object.
static defaultProps = {
width: 100
}
// Initial state is defined using a property initializer. In this simple
// form it behaves identical to TypeScript. You may refer to this.props
// within this initializer to make initial state a function of props.
state = {
counter: Math.round(this.props.width / 10)
}
// Instead of relying on auto-binding magic inside React, we use a property
// initializer with an arrow function. This effectively creates a single
// bound method per instance - the same as auto-binding.
handleClick = (event) => {
event.preventDefault();
this.setState({ counter: this.state.counter + 1 });
}
// Props, state and context are passed into render as a convenience to avoid
// the need for aliasing or referring to `this`.
render(props, state, context) {
return (
<div>
This button has been clicked: {state.counter} times
<button onClick={this.handleClick} style={{ width: props.width }} />
</div>
);
}
}