Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useRefValue hook #472

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions kotlin-react/src/main/kotlin/react/useRefValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@file:Suppress(
"NOTHING_TO_INLINE",
)

package react

import kotlin.reflect.KProperty

inline fun <T : Any> useRefValue(): RefDelegate<T?> =
useRef<T>()
.unsafeCast<RefDelegate<T?>>()

inline fun <T : Any> useRefValue(
initialValue: T,
): RefDelegate<T> =
useRef(initialValue)
.unsafeCast<RefDelegate<T>>()

// TODO: make external in IR
class RefDelegate<T>
private constructor() {
inline operator fun getValue(
thisRef: Nothing?,
property: KProperty<*>,
): T {
return asDynamic().current
}

inline operator fun setValue(
thisRef: Nothing?,
property: KProperty<*>,
value: T,
) {
asDynamic().current = value
}
}