Skip to content

A collection of simple, handy hooks (using side-effects) for Jetpack Compose so you don't have to create your own.

License

Notifications You must be signed in to change notification settings

CuriousNikhil/compose-hooks

Repository files navigation

Hooks for Jetpack Compose

A collection of small handy, ready-made hooks for Jetpack Compose so that you don't have to think about the states, logic or anything. Just plug it in and use!

jc-hooks-small

Inspired from react hooks.

useFetch

Add the following dependency in your build.gradle

  implementation("me.nikhilchaudhari:compose-usefetch:{latest-version}")

When you want just to fetch some data and don't want to setup everything unnecessarily!

For example -

  var resultState = useFetch(url = "https://yoururl.com/something")
  when(val data = resultState.value) {
       Result.Error -> { /* When there is some error, show Toast/snackbar/error message */  }
       Result.Response -> { /* When response is received successfully, show the data on UI */ }
       Result.Loading -> { /* When request is loading, show the loading progress on UI */ }
 }

usenetworkstate

Add the following dependency in your build.gradle

  implementation("me.nikhilchaudhari:compose-usenetworkstate:{latest-version}")

When you want to use the network connectivity as a state in Compose!

For example -

    val networkState by useNetworkState()

    if (networkState == NetworkState.Online) {
        // Call the main UI
    } else {
        // Show the network is disconnected/offline UI
        Text(text = "You are offline! Please connect to the network!")
    }

useReducer

Add the following dependency in your build.gradle

  implementation("me.nikhilchaudhari:compose-usereducer:{latest-version}")

When you want to make your state updates in Redux style.

For example -

    var count by remember { mutableStateOf(0) }
    
    val dispatcher = useReducer {
        "increment" does { count++ }
        "decrement" does { count-- }
    }
    
    Button(onClick = { dispatcher.dispatch("increment") }) { / ** / }

    Text(text = count.toString())

    Button(onClick = { dispatcher.dispatch("decrement") }) {/**/}
  

Checkout more examples here in the sample app. You can go to individual hook repository to know more about those.

Want to publish your own compose-hook?

Please feel free to contribute. Create your own hooks. Checkout the contribution guide for more.

Reach out to me

Website | Twitter | Mail | LinkedIn

License

LICENSE Apache 2.0

About

A collection of simple, handy hooks (using side-effects) for Jetpack Compose so you don't have to create your own.

Topics

Resources

License

Stars

Watchers

Forks

Languages