-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-baseclass-mixins.js
78 lines (72 loc) · 2.17 KB
/
code-baseclass-mixins.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict"
require("./node_modules/bootstrap/dist/css/bootstrap.min.css")
import React, { Component, PropTypes, DefaultProps } from 'react'
import ReactDOM from 'react-dom'
// React is phasing out mixins
// 1 alternative way is to create a base class
export default class BaseContent extends Component {
componentWillMount (){
console.log('BaseContent: componentWillMount is triggered')
}
componentDidMount (e){
console.log('BaseContent: componentDidMount is triggered')
console.log('DOM node: ', ReactDOM.findDOMNode(this))
}
componentWillReceiveProps (newProps){
console.log('BaseContent: componentWillReceiveProps is triggered')
console.log('new props: ', newProps)
}
shouldComponentUpdate (newProps, newState){
console.log('BaseContent: shouldComponentUpdate is triggered')
console.log('new props: ', newProps)
console.log('new state: ', newState)
return true
}
componentWillUpdate (newProps, newState){
console.log('BaseContent: componentWillUpdate is triggered')
console.log('new props: ', newProps)
console.log('new state: ', newState)
}
componentDidUpdate (oldProps, oldState){
console.log('BaseContent: componentDidUpdate is triggered')
console.log('new props: ', oldProps)
console.log('old props: ', oldState)
}
componentWillUnmount (){
console.log('BaseContent: componentWillUnmount')
}
_onClickHandler (e) {
e.preventDefault();
console.log("BaseContent: _onClickHandler called by: " + e.currentTarget.id)
}
}
// Components can then extend the base class
// remeber to call super to instantiate the parent class
export default class Content extends BaseContent {
constructor (props) {
super(props)
}
render() {
return (
<div>
<h2>React extend base class</h2>
<Content2 />
</div>
)
}
}
// parent base class methods can be accessed with 'this.methodName'
export default class Content2 extends BaseContent {
constructor (props) {
super(props)
}
render() {
return (
<a id="content2Link" href="#" onClick={this._onClickHandler}>Base class can replace mixins for ES6 classes..</a>
)
}
}
ReactDOM.render(
<Content />,
document.getElementById('content')
);