-
Notifications
You must be signed in to change notification settings - Fork 164
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
Consistency of hook dependencies #463
Comments
@Skolotsky @turansky WDYT? |
i like vararg version |
I use/like vararg version only :) inline fun useEffectOnce(
noinline effect: () -> RCleanup,
) {
rawUseEffect(effect, emptyArray())
}
inline fun useEffect(
vararg dependencies: dynamic,
noinline effect: () -> RCleanup,
) {
rawUseEffect(effect, dependencies)
}
// super draft name
inline fun useSimpleEffect(
vararg dependencies: dynamic,
noinline effect: () -> Unit,
) {
rawUseEffect(effect, dependencies)
} Sync with cc @vlsi |
Also var count by useState(3)
useEffect(count) {
val timerId = window.setTimeout({ count += 1 }, 2000)
cleanup { window.clearTimeout(timerId) }
} With effect builder we will have:
Without effect builder we will have |
@sgrishchenko please create a PR |
To be honest, I have initiated this issue because now there is not any mechanism to protect user from missing some dependencies in hooks like So, I have thought more and better about hooks API and I have changed my mind: now I am proposing to use Arrays instead Varargs (Array is first, Callback is last): Pros of Arrays:
Cons of Arrays:
And now everyone can see than it is not just random data, it is dependencies, and, it seems I can't do the same with Vararg. |
I have prepared PR with implementation of my suggestions and I have added EffectBuilder, thanks for @turansky, it looks really cool. I also added KDocs with examples and deprecation annotations with replacements (where it is possible to generate code without compile errors) and suggestions how to migrate to new API. See #468 |
Formatting is improved
Now there are three ways to use dependencies of hooks:
useCallback({ value }, arrayOf(value))
useCallback(value) { value }
useEffect(listOf(value)) { println(value) }
I suggest to use only one form - Vararg (dependencies are first). I think it is most compact form and Kotlin friendly way (because callbacks are last parameter in this case).
I can create PR, where I will add overloads for this kind of API and mark all other form as deprecated for backward compatibility.
The text was updated successfully, but these errors were encountered: