-
Notifications
You must be signed in to change notification settings - Fork 11
/
react_lifecycle.go
158 lines (137 loc) · 5.59 KB
/
react_lifecycle.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2018 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package react
import (
"github.com/gopherjs/gopherjs/js"
)
const (
// TODO: add type checking props
getDefaultProps = "getDefaultProps"
// Mounting
getInitialState = "getInitialState"
getDerivedStateFromProps = "getDerivedStateFromProps"
render = "render"
componentDidMount = "componentDidMount"
// Updating
shouldComponentUpdate = "shouldComponentUpdate"
getSnapshotBeforeUpdate = "getSnapshotBeforeUpdate"
componentDidUpdate = "componentDidUpdate"
// Unmounting
componentWillUnmount = "componentWillUnmount"
// Error-handling
componentDidCatch = "componentDidCatch"
)
// GetDefaultProps sets the getDefaultProps method.
func (def ClassDef) GetDefaultProps(f func(this *js.Object) interface{}) {
def.SetMethod(getDefaultProps, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
return SToMap(f(this))
})
}
// GetInitialState sets the getInitialState method.
// Note: It is usually not recommended to use the props when setting the initial state.
func (def ClassDef) GetInitialState(f func(this *js.Object, props Map) interface{}) {
def.SetMethod(getInitialState, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
return SToMap(f(this, props))
})
}
// GetDerivedStateFromProps sets the getDerivedStateFromProps class method.
//
// See: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
func (def ClassDef) GetDerivedStateFromProps(f func(nextProps, prevState Map) interface{}) {
def.setMethod(true, getDerivedStateFromProps, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
nextProps := func(key string) *js.Object {
return arguments[0].Get(key)
}
prevState := func(key string) *js.Object {
return arguments[1].Get(key)
}
return SToMap(f(nextProps, prevState))
})
}
// ComponentDidMount sets the componentDidMount method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidmount
func (def ClassDef) ComponentDidMount(f func(this *js.Object, props, state Map, setState SetState)) {
def.SetMethod(componentDidMount, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
f(this, props, state, setState)
return nil
})
}
// ComponentWillUnmount sets the componentWillUnmount method.
//
// See: https://reactjs.org/docs/react-component.html#componentwillunmount
func (def ClassDef) ComponentWillUnmount(f func(this *js.Object, props, state Map)) {
def.SetMethod(componentWillUnmount, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
f(this, props, state)
return nil
})
}
// ShouldComponentUpdate sets the shouldComponentUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#shouldcomponentupdate
func (def ClassDef) ShouldComponentUpdate(f func(this *js.Object, props, nextProps, state, nextState Map) bool) {
def.SetMethod(shouldComponentUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
nextProps := func(key string) *js.Object {
return arguments[0].Get(key)
}
nextState := func(key string) *js.Object {
return arguments[1].Get(key)
}
return f(this, props, nextProps, state, nextState)
})
}
// GetSnapshotBeforeUpdate sets the getSnapshotBeforeUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate
func (def ClassDef) GetSnapshotBeforeUpdate(f func(this *js.Object, prevProps, props, prevState, state Map) interface{}) {
def.SetMethod(getSnapshotBeforeUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
prevProps := func(key string) *js.Object {
return arguments[0].Get(key)
}
prevState := func(key string) *js.Object {
return arguments[1].Get(key)
}
ret := f(this, prevProps, props, prevState, state)
if ret == nil {
return nil
} else if isStruct(ret) {
return convertStruct(ret)
} else {
return ret
}
})
}
// ComponentDidUpdate sets the componentDidUpdate method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidupdate
func (def ClassDef) ComponentDidUpdate(f func(this *js.Object, prevProps, props, prevState, state Map, setState SetState, snapshot *js.Object)) {
def.SetMethod(componentDidUpdate, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
snapshot := arguments[2]
prevProps := func(key string) *js.Object {
return arguments[0].Get(key)
}
prevState := func(key string) *js.Object {
return arguments[1].Get(key)
}
f(this, prevProps, props, prevState, state, setState, snapshot)
return nil
})
}
// Render sets the render method.
//
// See: https://reactjs.org/docs/react-component.html#render
func (def ClassDef) Render(f func(this *js.Object, props, state Map) interface{}) {
def.SetMethod(render, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
return f(this, props, state)
})
}
// ComponentDidCatch sets the componentDidCatch method.
//
// See: https://reactjs.org/docs/react-component.html#componentdidcatch
func (def ClassDef) ComponentDidCatch(f func(this *js.Object, err, info *js.Object, props, state Map, setState SetState)) {
def.SetMethod(componentDidCatch, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
err := arguments[0]
info := arguments[1]
f(this, err, info, props, state, setState)
return nil
})
}