iari
05/08/2020, 6:09 PMRComponentClassStatics
and RStatics
and some 'documentation' here:
https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-react/src/main/kotlin/react/React.kt#L63-L77
So i tried implementing it like this:
external interface ErrorBoundaryState : RState {
var error: Throwable?
}
class ErrorBoundary : RComponent<RProps, ErrorBoundaryState>() {
init {
state.error = null;
}
override fun componentDidCatch(error: Throwable, info: RErrorInfo) {
//super.componentDidCatch(error, info)
//state.error = error
//state.errinfo = info
console.log("ErrorBoundary encountered error:", error)
}
override fun RBuilder.render() {
state.error?.let { e ->
styledDiv {
h3 {
+"Something went wrong"
}
p {
+e.toString()
}
}
} ?: props.children()
}
companion object : RStatics<RProps, ErrorBoundaryState, ErrorBoundary, Nothing>(ErrorBoundary::class) {
override var getDerivedStateFromError: ((Throwable) -> ErrorBoundaryState?)?
get() = {
object : ErrorBoundaryState {
override var error: Throwable?
get() = it
set(value) {}
}
}
set(value) {
}
}
}
However, this fails and nothing is rendered, and I get this error:
Warning: ErrorBoundary: Error boundaries should implement getDerivedStateFromError(). In that method, return a state update to display an error message or fallback UI.
in ErrorBoundary
in Unknown
so how is this done properly?iari
05/08/2020, 6:19 PMcompanion object : RStatics<RProps, ErrorBoundaryState, ErrorBoundary, Nothing>(ErrorBoundary::class) {
init {
getDerivedStateFromError = {
object : ErrorBoundaryState {
override var error: Throwable? = it
}
}
}
}
But is still does not work.turansky
05/08/2020, 6:23 PMgetDerivedStateFromProps = { p, s -> s }
iari
05/08/2020, 6:31 PMiari
05/08/2020, 6:32 PMcompanion object : RStatics<RProps, ErrorBoundaryState, ErrorBoundary, Nothing>(ErrorBoundary::class) {
init {
getDerivedStateFromError = {
object : ErrorBoundaryState {
override var error: Throwable? = it
}
}
getDerivedStateFromProps = { p, s -> s }
}
}
this is what the code looks like nowturansky
05/08/2020, 6:32 PMturansky
05/08/2020, 6:32 PMiari
05/08/2020, 6:34 PMturansky
05/08/2020, 6:35 PMgetDerivedStateFromError = { t -> js<ErrorBoundaryState>{
throwable = t
} }
turansky
05/08/2020, 6:36 PMfromProps
can be removediari
05/08/2020, 6:36 PMturansky
05/08/2020, 6:37 PMgetDerivedStateFromError = { t -> jsObject<ErrorBoundaryState>{
throwable = t
} }
turansky
05/08/2020, 6:39 PMiari
05/08/2020, 6:39 PMiari
05/08/2020, 6:39 PMiari
05/08/2020, 6:42 PMerrorBoundary {
p {
+"Test"
throw RuntimeException("whatever")
}
}
iari
05/08/2020, 6:42 PMfun RBuilder.errorBoundary(handler: RBuilder.() -> Unit): ReactElement = child(ErrorBoundary::class, handler)
iari
05/08/2020, 6:43 PMexternal interface ErrorBoundaryState : RState {
var error: Throwable?
}
class ErrorBoundary : RComponent<RProps, ErrorBoundaryState>() {
override fun componentDidCatch(error: Throwable, info: RErrorInfo) {
console.log("ErrorBoundary encountered error:", error)
}
override fun RBuilder.render() {
val error = state.error
if (error != null) {
styledDiv {
h3 {
+"Something went wrong"
}
p {
+error.toString()
}
}
} else props.children()
}
companion object : RStatics<RProps, ErrorBoundaryState, ErrorBoundary, Nothing>(ErrorBoundary::class) {
init {
getDerivedStateFromError = {
jsObject<ErrorBoundaryState> {
error = it
}
}
}
}
}
turansky
05/08/2020, 6:44 PMiari
05/08/2020, 6:45 PMturansky
05/08/2020, 6:46 PMiari
05/08/2020, 6:48 PMturansky
05/08/2020, 6:48 PMturansky
05/08/2020, 6:48 PM} else props.children()
iari
05/08/2020, 6:50 PMiari
05/08/2020, 6:50 PMoverride fun RBuilder.render() {
val error = state.error
if (error != null) {
styledDiv {
h3 {
+"Something went wrong"
}
p {
+error.toString()
}
}
} else {
console.log("renderind children")
props.children()
}
}
got no "renderind children" log statement printed.turansky
05/08/2020, 6:53 PMiari
05/08/2020, 6:53 PMturansky
05/08/2020, 6:53 PMiari
05/08/2020, 6:53 PMiari
05/08/2020, 6:54 PMturansky
05/08/2020, 6:54 PMturansky
05/08/2020, 6:55 PMjsObject
builder for itturansky
05/08/2020, 6:55 PMobject :
required