Joost Klitsie
09/28/2020, 3:07 PMfun <T> CommonAttributeGroupFacade.onClick(argument: T, func: (T) -> Unit) {
onClickFunction = useCallback(
callback = {
func(argument)
},
dependencies = arrayOf(argument)
)
}
var CommonAttributeGroupFacade.onClick: () -> Unit
get() = throw UnsupportedOperationException("You can't read variable onClick")
set(newValue) {
onClickFunction = useCallback(
callback = {
newValue()
},
dependencies = emptyArray()
)
}
var CommonAttributeGroupFacade.onTextChange: (String) -> Unit
get() = throw UnsupportedOperationException("You can't read variable onChange")
set(newValue) {
onChangeFunction = useCallback(
callback = { event: Event ->
newValue(event.target.asDynamic().value as String)
},
dependencies = emptyArray()
)
}
The only problem I have is: if I have a list where I can add/remove items, where the list items use one of these (for example onclick), I will get (logically) errors about not having the same amount of hooks when I add/remove list items. To solve that problem, I created a useList method that wraps the list in a functional component and gets recreated on data changes:
fun <T : BaseListItem> RBuilder.useList(
list: List<T>,
vararg classMap: Pair<ListStyle, String>,
block: ListElementBuilder<UL, ListProps>.(List<T>) -> Unit
) {
useCallback(
functionalComponent<RProps> { list(classMap = *classMap, block = { block(list) }) },
arrayOf(list)
).also { child(it) }
}
Which gives the issue of having a sluggish list during a lot of interactions (for example typing into a list element).araqnid
09/28/2020, 6:13 PM