Skip to content

[Feature request] Change the confusing markerState parameter position to a clear name. #637

@JSpiner

Description

@JSpiner

MarkerState has rememberMarkerState for use in compose.

@Composable
public fun rememberMarkerState(
    key: String? = null,
    position: LatLng = LatLng(0.0, 0.0)
): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
    MarkerState(position)
}
// wrong usage example
@Composable
public fun MyGoogleMapScreen(stationPosition: LatLng) {
    val myMarkerState = rembmerMarkerState(position = stationPosition)
}

But looking at the example above, it's easy to get confused.
This is because there is a risk of misunderstanding that the value entered as a parameter to remember (position in this case) acts as a key that is automatically reflected when the value changes.
I also had a hard time because the position wasn't updated.
To avoid this misunderstanding, compose foundation libraries add an inital prefix to rememberXXX functions.

// compose library example
// rememberScrollState from `androidx.compose.foundation`
@Composable
fun rememberScrollState(initial: Int = 0): ScrollState {
    return rememberSaveable(saver = ScrollState.Saver) {
        ScrollState(initial = initial)
    }
}

// rememberLazyListState from `androidx.compose.foundation.lazy`
@Composable
fun rememberLazyListState(
    initialFirstVisibleItemIndex: Int = 0,
    initialFirstVisibleItemScrollOffset: Int = 0
): LazyListState {
    return rememberSaveable(saver = LazyListState.Saver) {
        LazyListState(
            initialFirstVisibleItemIndex,
            initialFirstVisibleItemScrollOffset
        )
    }
}

// rememberLazyGridState from `androidx.compose.foundation.lazy.grid`
@Composable
fun rememberLazyGridState(
    initialFirstVisibleItemIndex: Int = 0,
    initialFirstVisibleItemScrollOffset: Int = 0
): LazyGridState {
    return rememberSaveable(saver = LazyGridState.Saver) {
        LazyGridState(
            initialFirstVisibleItemIndex,
            initialFirstVisibleItemScrollOffset
        )
    }
}

So my suggestion is to change the name from position to initialPosition to reduce confusion.
It seems like a simple fix, I'll create a PR. Please review it.
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    releasedtriage meI really want to be triaged.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions