Skip to content

Commit

Permalink
- add getDerivedStateFromError static method
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketlaunchr-cto committed Sep 23, 2019
1 parent 0b7b6d5 commit bdb637e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions react_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const (
componentWillUnmount = "componentWillUnmount"

// Error-handling
componentDidCatch = "componentDidCatch"
componentDidCatch = "componentDidCatch"
getDerivedStateFromError = "getDerivedStateFromError"
)

// GetDefaultProps sets the getDefaultProps method.
Expand All @@ -46,7 +47,7 @@ func (def ClassDef) GetInitialState(f func(this *js.Object, props Map) interface
// 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{}) {
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{} {

Expand Down Expand Up @@ -156,3 +157,13 @@ func (def ClassDef) ComponentDidCatch(f func(this *js.Object, err, info *js.Obje
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))
})
}

0 comments on commit bdb637e

Please sign in to comment.