Skip to content

reduxkotlin/redux-kotlin-compose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6270d49 · Jan 11, 2023

History

24 Commits
Jan 11, 2023
Jan 11, 2023
Jan 11, 2023
Jan 11, 2023
Aug 9, 2022
Aug 8, 2022
Aug 8, 2022
Aug 8, 2022
Aug 8, 2022
Jan 11, 2023
Jan 11, 2023
Aug 8, 2022
Aug 8, 2022
Jan 11, 2023

Repository files navigation

Check badge badge badge Slack chat Dokka docs Version maven-central

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)
    }
  )
}