ComposeHooks
English | 简体中文

Star History

KMP Support
NOTE: The artifact id is hooks2
implementation("xyz.junerver.compose:hooks2:<latest_release>")
Supported targets:
- android
- desktop (jvm)
- iosarm64
- iosimulatorarm64
- iosx64
- wasmJs (browser) — enables Compose Multiplatform on the Web (WASM), so downstream projects can run interactive previews in the browser. Available since
2.4.0.
About
The idea for the project comes from alibaba/hooks, which is a very easy-to-use collection of React Hooks.
It encapsulates most common operations as custom hooks, and useRequest is the top priority. It is
designed to be very lightweight, highly configurable, and easy to use.
Therefore, based on this design idea, Hooks that can be used in the Compose project were created
using similar API names.
The hooks that have been implemented so far are as follows:
Note: All use functions also have the signature of remember. If you prefer Compose’s naming method, just use rememberXxx!
Hooks
State
Effect
LifeCycle
| hook name | description |
|---|
| useMount | A hook that executes a function after the component is mounted. |
| useUnmount |
Time
Math
Utilities
Functions marked with * can only be used on Android
AI Module
A separate AI module providing hooks for AI chat completions and structured data generation with OpenAI-compatible APIs and Anthropic Messages API.
Add AI module dependency:
implementation("xyz.junerver.compose:ai:<latest_release>")
Features:
- Streaming responses (SSE) with real-time typewriter effect
- Non-streaming mode support (
stream = false)
- Multimodal input support (text, images, files)
- Structured data generation with type safety
- Message state management
- Loading and error states
- Control functions (send, stop, reload)
- Configurable options (temperature, maxTokens, timeout, etc.)
- Lifecycle callbacks (onFinish, onError, onStream)
Built-in Providers:
Note: MiMo API returns tool calls in XML format (instead of standard OpenAI tool_calls array). This library automatically handles this format.
Example - Chat:
val (messages, isLoading, error, sendMessage, _, _, reload, stop) = useChat {
provider = Providers.OpenAI(apiKey = "your-api-key")
model = "gpt-3.5-turbo"
systemPrompt = "You are a helpful assistant."
onFinish = { message, usage, reason ->
println("Completed: ${message.content}")
}
}
sendMessage("Hello!")
messages.value.forEach { message ->
Text("${message.role}: ${message.content}")
}
Example - Generate Object:
@Serializable
data class Recipe(val name: String, val ingredients: List<String>)
val (recipe, rawJson, isLoading, error, submit, stop) = useGenerateObject<Recipe>(
schema = Recipe::class.jsonSchemaString,
) {
provider = Providers.OpenAI(apiKey = "your-api-key")
systemPrompt = "You are a professional chef."
}
submit()
recipe.value?.let { r ->
Text()
r.ingredients.forEach { Text() }
}
Add to dependencies
KMP project
implementation 'xyz.junerver.compose:hooks2:<latest_release>'
implementation("xyz.junerver.compose:hooks2:<latest_release>")
Use hooks2 In Android
For pure Android projects, please use the following dependencies(Artifact id:hooks2-android):
implementation("xyz.junerver.compose:hooks2-android:<latest_release>")
If used in ComposeMultiplatform, use artifact id: hooks2
Old version hooks continue to support
If your project does not have performance issues due to recompose , you can continue to use the old version in the Android project, and the bugfix will be synchronized to the old version in subsequent development.
implementation("xyz.junerver.compose:hooks:2.0.3")
Quick Setup
For more usage, please refer to wiki and examples
Live Templates
Copy hooks in the Live Templates directory
File, paste into C:\Users\<user-name>\AppData\Roaming\Google\AndroidStudio2023.2\templates\
You can easily create code snippets of useState and useRequest through us and ur.
Open Inlay Hints for Kotlin Type
For hooks like useRequest, its return value can deconstruct many objects and functions. It is necessary to enable InlayHint:
Editor - Inlay Hints - Types - Kotlin
ProGuard
If you are using ProGuard you might need to add the following option:
-keep class xyz.junerver.composehooks.** { *; }
-keepclassmembers class xyz.junerver.composehooks.** { *; }
-dontwarn xyz.junerver.composehooks.**
Documentation
Todo:
KMP friendly
CI
- Unit Test
- Complete documentation
参考/Thanks
- alibaba/hooks
- pavi2410/useCompose
- vueuse/vueuse
Contributing Guidelines
Contributing guidelines
License
Apache License 2.0