Skip to content

Add StateParam rule#577

Merged
mrmans0n merged 1 commit intomainfrom
nacho/state-param
Jan 16, 2026
Merged

Add StateParam rule#577
mrmans0n merged 1 commit intomainfrom
nacho/state-param

Conversation

@mrmans0n
Copy link
Copy Markdown
Owner

@mrmans0n mrmans0n commented Jan 16, 2026

Compose API Guidelines for components states that:

State<T> as a parameter

Parameters of type State<T> are discouraged since it unnecessarily narrows the type of objects that can be passed in the function. Given param: State<Float>, there are two better alternatives available, depending on the use case:

  1. param: Float. If the parameter doesn’t change often, or is being read immediately in the component (composition), developers can provide just a plain parameter and recompose the component when it changes.
  2. param: () -> Float. To delay reading the value until a later time via param.invoke(), lambda might be provided as a parameter. This allows the developers of the component to read the value only when/if it is needed and avoid unnecessary work. For example, if the value is only read during drawing operation, only redraw will occur. This leaves the flexibility to the user to provide any expression, including the State<T>’s read:
    1. param = { myState.value } - read the State<T>’s value
    2. param = { justValueWithoutState } - plain value not backed by the State<T>
    3. param = { myObject.offset } - user can have a custom state object where the field (e.g. offset) is backed by the mutableStateOf()

DON’T

fun Badge(position: State<Dp>) {}

// not possible since only State<T> is allowed
Badge(position = scrollState.offset) // DOES NOT COMPILE

Do:

fun Badge(position: () -> Dp) {}

// works ok
Badge(position = { scrollState.offset })

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 16, 2026

PR Preview Action v1.8.0
Preview removed because the pull request was closed.
2026-01-16 16:33 UTC

@mrmans0n mrmans0n merged commit 4dd6fa9 into main Jan 16, 2026
6 checks passed
@mrmans0n mrmans0n deleted the nacho/state-param branch January 16, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant