reduxkotlin / redux-kotlin-compose

Compose Multiplatform integration for Redux-Kotlin
https://reduxkotlin.github.io/redux-kotlin-compose
29 stars 1 forks source link
compose-multiplatform kotlin-multiplatform redux-kotlin

Releasebadge badge badge Slack chat Dokka docs Version maven-central Version maven-snapshot

Redux-Kotlin-Compose

Compose Multiplatform integration for Redux Kotlin

Installation

Artifacts are hosted on maven central. For multiplatform, add the following to your shared module:

kotlin {
  sourceSets {
    commonMain {
      dependencies {
        implementation("org.reduxkotlin:redux-kotlin-compose:_")
      }
    }
  }
}

For JVM only:

dependencies {
  implementation("org.reduxkotlin:redux-kotlin-compose-jvm:_")
}

Usage

data class State(val name: String? = null)
sealed interface Action {
  data class Rename(val name: String) : Action
  object ClearName : Action
}

val reducer: Reducer<State> = reducerForActionType<State, Action> { state, action ->
  when (action) {
    is Action.Rename -> state.copy(name = action.name)
    is Action.ClearName -> state.copy(name = null)
  }
}

@Composable
fun App() {
  StoreProvider(createStore(reducer, State())) {
    Component()
  }
}

@Composable
fun Component() {
  val name by selectState<State, String> { name }
  val dispatch = rememberDispatcher()
  Text(name)
  Button(
    text = "Clear",
    onClick = {
      dispatch(ClearName)
    }
  )
}