Using React, is it possible to have a function in ...
# javascript
a
Using React, is it possible to have a function in an RState interface that modifies additional state based on logic? I'm converting a JS example to Kotlin and they wrap all the state in a custom hook with at least 1 inner function that conditionally set some state values based on logic. I tried doing that in Kotlin and it didn't seem to work. Not a big deal but seems like a cleaner way to separate code by having state logic directly in the interface that defines it. An example below. setState { someFunction(newValue) // member of RState interface that sets 1 or more values }
i
You can use hooks in Kotlin/JS too.
RState
interface effectively external interface, because use plain javascript object. And react can modify this object, as it wants. So it have additional functions only if they have it in real JavaScript world. You can declare this function as a extension function. Something like
Copy code
fun RYourState.someFunction(someValue: Type): Type {
...
}
s
Also, remember that if you set state conditional on some other state you should use the transform state variant
a
Changing from interface function to extension function seemed to work. Whats the transform state variant?
Its this one in the react wrapper:
fun setState(transformState: (S) -> S, callback: () -> Unit = _definedExternally_)