Hello! Is there a way to extend `State` . I want t...
# javascript
n
Hello! Is there a way to extend
State
. I want to create function which changes state in different Component classes.
Copy code
// external interface as example, but it will not work
external interface StateWithError: State {
  var error: Throwable?
}

fun <S : StateWithError> Component<*, S>.someFunction(){
  setState { 
    error = Throwable("Some msg")
  }
}
I tried to use
external interface
and
abstract class
(even with
@JsName("error")
), but always getting name mangling. Is there a way to create such function?
1
g
Did you try with another name than
error
? I'm thinking it could be a reserved word.
t
IR?
n
@Grégory Lureau no, it doesn't work.
@turansky yes
t
Could you check it in legacy?
n
In legacy it throws
Cannot read properties of undefined (reading 'pass')
which likely related to another problem
without calling the function
t
Time for function components 🙂
n
Came up with this solution. In this case it can work with mutable fields of data class.
Copy code
data class WrapError(var error: Throwable?)

interface StateWithError: State {
  var wrapError: WrapError
}

inline fun <S: StateWithError> Component<*, S>.setError(wrapError: WrapError) {
  setState {
    wrapError.error = Throwable("Say hello to my little friend")
  }
}
1