-
Notifications
You must be signed in to change notification settings - Fork 11
/
react_lifecycle.go
178 lines (155 loc) · 6.38 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package react
import (
"github.com/gopherjs/gopherjs/js"
)
const (
_propTypes = "propTypes"
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"
getDerivedStateFromError = "getDerivedStateFromError"
)
// GetDefaultProps sets the getDefaultProps method.
//
// See: https://reactjs.org/docs/react-without-es6.html#declaring-default-props
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))
})
}
// SetPropTypes is used to typecheck and validate props.
//
// See: https://reactjs.org/docs/typechecking-with-proptypes.html
func (def ClassDef) SetPropTypes(propTypes interface{}) {
def[_propTypes] = SToMap(propTypes)
}
// 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(props, state 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
})
}
// GetDerivedStateFromError sets the getDerivedStateFromError class method.
//
// See: https://reactjs.org/docs/react-component.html#static-getderivedstatefromerror
func (def ClassDef) GetDerivedStateFromError(f func(err *js.Object) interface{}) {
def.setMethod(true, getDerivedStateFromError, func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
err := arguments[0]
return SToMap(f(err))
})
}