-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentAPI.jsx
64 lines (58 loc) · 1.97 KB
/
ComponentAPI.jsx
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* React - Component API
* Here, we will discuss 3 methods: - setState(), forceUpdate() and ReactDOM.findDOMNode()
* In new ES6 classes, we have to manually bind this.
*/
import React from 'react';
import ReactDOM from 'react-dom';
class ComponentAPI extends React.Component {
constructor () {
super();
this.state = {
data: []
}
this.setStateHandler = this.setStateHandler.bind(this);
this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
this.findDomNodeHandler = this.findDomNodeHandler.bind(this);
};
setStateHandler() {
var item = "setState...";
var myArray = this.state.data.slice();
myArray.push(item);
/**
* Set State
* setState() method is used to update the state of component. This method will not replace
* the state, but only add changes to original state.
*/
this.setState({ data: myArray })
};
forceUpdateHandler() {
/**
* Force Update
* Sometimes we might want to update the component manually. This can be achieved using
* forceUpdate() method.
*/
this.forceUpdate();
};
findDomNodeHandler() {
var myDiv = document.getElementById('myDiv');
/**
* Find DOM Node
* For DOM manipulation, we can use ReactDOM.findDOMNode() method.
*/
ReactDOM.findDOMNode(myDiv).style.color = 'green';
};
render() {
return (
<div>
<button onClick = { this.setStateHandler }>Set State</button>
<h4>State Array: { this.state.data }</h4>
<button onClick = { this.forceUpdateHandler }>Force Update</button>
<h4>Random number: { Math.random() }</h4>
<button onClick = { this.findDomNodeHandler }>Find DOM Node</button>
<div id="myDiv">Node</div>
</div>
);
}
}
export default ComponentAPI;