Big Chungus
06/14/2020, 4:08 PMuseState
hook in pure kotlin? Or does it rely on JS's var
magic?
Posted in #reactturansky
06/14/2020, 4:12 PMin pure KotlinWhat does it mean?
Big Chungus
06/14/2020, 4:12 PMturansky
06/14/2020, 4:27 PMBig Chungus
06/14/2020, 4:28 PMturansky
06/14/2020, 4:28 PMturansky
06/14/2020, 4:29 PMBig Chungus
06/14/2020, 4:31 PMturansky
06/14/2020, 4:31 PMBig Chungus
06/14/2020, 4:34 PMBig Chungus
06/14/2020, 4:35 PMtypealias SetState<T> = (value: T) -> Unit
operator fun <T> Pair<T, SetState<T>>.getValue(thisRef: Any?, property: KProperty<*>): T {
return first
}
operator fun <T> Pair<T, SetState<T>>.setValue(thisRef: Any?, property: KProperty<*>, value: T) {
second(value)
}
private val store = mutableListOf<Any?>()
private var index = 0
fun <T> useState(initValue: T): Pair<T, SetState<T>> {
@Suppress("UNCHECKED_CAST")
val currentState = (store[index] ?: initValue) as T
val setState = fun(): SetState<T> {
val currentIndex = index
return fun(value:T) {
store[currentIndex] = value
}
}()
index++
return currentState to setState
}
fun usageExample() {
val (text, setText) = useState("none")
val count by useState(0)
}
turansky
06/14/2020, 4:35 PMMail showstopper here is how you ensure you get the same value when calling it in functional component each render.You can use
provideDelegate
if you need common state for delegatesturansky
06/14/2020, 4:37 PMBig Chungus
06/14/2020, 4:39 PMturansky
06/14/2020, 4:40 PMturansky
06/14/2020, 4:42 PMBig Chungus
06/14/2020, 4:43 PMturansky
06/14/2020, 4:49 PMNothing?
for delegatesturansky
06/14/2020, 4:49 PMturansky
06/14/2020, 4:53 PMBig Chungus
06/14/2020, 4:56 PMturansky
06/14/2020, 5:01 PMBig Chungus
06/14/2020, 5:02 PMimport kotlin.reflect.*
typealias SetState<T> = (value: T) -> Unit
operator fun <T> Pair<T, SetState<T>>.getValue(thisRef: Any?, property: KProperty<*>): T {
return first
}
operator fun <T> Pair<T, SetState<T>>.setValue(thisRef: Any?, property: KProperty<*>, value: T) {
second(value)
}
private val store = mutableMapOf<Int, Any?>()
var index = 0
fun <T> useState(initValue: T): Pair<T, SetState<T>> {
@Suppress("UNCHECKED_CAST")
val currentState = (store[index] ?: initValue) as T
store[index]= currentState
val setState = fun(): SetState<T> {
val currentIndex = index
return fun(value: T) {
store[currentIndex] = value
}
}()
index++
return currentState to setState
}
Big Chungus
06/14/2020, 5:02 PMturansky
06/14/2020, 6:26 PMturansky
06/14/2020, 6:45 PMBig Chungus
06/14/2020, 6:47 PMBig Chungus
06/15/2020, 7:41 AMBig Chungus
06/15/2020, 7:42 AMHamza
06/15/2020, 7:43 AMturansky
06/15/2020, 8:01 AMPair
as result looks ugly in this case
2. Extensions on lambdas too
You can encapsulate logic with delegate
get
+ set
encapsulation - classic delegate caseturansky
06/15/2020, 9:04 AM// Now
val setState = fun(): SetState<T> {
return fun(value: T) {
store[currentIndex] = value
}
}()
// Expected
val setState: SetState<T> = {
store[currentIndex] = it
}
Big Chungus
06/15/2020, 9:41 AMval (state, setState) = useState(1)
You can still use it as
val state by useState(1)
with my current solutionBig Chungus
06/15/2020, 9:41 AMturansky
06/18/2020, 4:39 PMcomponent
operatorsBig Chungus
06/18/2020, 4:41 PMturansky
06/18/2020, 4:43 PMturansky
06/18/2020, 4:43 PMBig Chungus
06/18/2020, 4:44 PM