how can I add `modifier.clickable` only if a varia...
# compose
p
how can I add
modifier.clickable
only if a variable is different from null in the composable?
a
You can apply it conditionally. I usually do this by:
Copy code
Modifier
   .let { if(condition) it.clickable{} else it }
p
is that better than using this?
Copy code
.then(if (enabled) Modifier.clickable(onClick = {}) else Modifier)
z
The latter is more idiomatic, but they are both correct
a
Modifier.clickable(enabled = enabled,onClick = {}) no need for extra lambda :)
👆 1
v
You might benefit from a
ifTrue
Modifier utility in your codebase. Very useful and you will end up using it all over your codebase. Covered in more detail here - https://www.jetpackcompose.app/snippets/ifTrueModifier
The
ifNotNull
Modifier might be the more direct one for your need but I find the
ifTrue
one to be more useful for all use cases - https://www.jetpackcompose.app/snippets/IfNotNullModifier
202 Views