Package-level declarations

Types

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
data class CountdownOptions

Countdown options

Link copied to clipboard
data class CounterOptions
Link copied to clipboard
data class DebounceOptions

Debounce options

Link copied to clipboard
typealias Dispatch<A> = (A) -> Unit
Link copied to clipboard
typealias DispatchAsync<A> = (block: suspend CoroutineScope.(Dispatch<A>) -> A) -> Unit
Link copied to clipboard
data class FormattedRes(val days: Int, val hours: Int, val minutes: Int, val seconds: Int, val milliseconds: Int)
Link copied to clipboard
data class IntervalOptions

Interval options

Link copied to clipboard
typealias Middleware<S, A> = (dispatch: Dispatch<A>, state: S) -> Dispatch<A>
Link copied to clipboard
class MutableRef<T>(initialValue: T) : Ref<T>

Mutable ref

Link copied to clipboard
abstract class Options<T>(val creator: () -> T)

Description: 规范Options的形式

Link copied to clipboard
interface ReactContext<T>
Link copied to clipboard
typealias Reducer<S, A> = (prevState: S, action: A) -> S
Link copied to clipboard
sealed interface Ref<T>

Read-only Ref interface

Link copied to clipboard
data class ThrottleOptions

Throttle options

Link copied to clipboard
data class TimestampOptions

Description: useTimestampuseTimestampRef的配置项

Link copied to clipboard
data class UndoState<T>(var past: PersistentList<T> = persistentListOf(), var present: T, var future: PersistentList<T> = persistentListOf())
Link copied to clipboard
data class UseNowOptions(var interval: Duration = 1.seconds, var format: (Long) -> String? = null)

Properties

Link copied to clipboard
val PersistentContext: ReactContext<Triple<PersistentGet, PersistentSave, PersistentClear>>

By default, CacheManager.cache is used for memory persistence. usePersistent is a lightweight encapsulation, you need to provide your own persistence solution globally through PersistentContext.Provider;

Functions

Link copied to clipboard
fun <T> _rememberGetState(default: T): Triple<T, SetValueFn<T>, GetValueFn<T>>
Link copied to clipboard
Link copied to clipboard
fun <T> _useGetState(default: T): Triple<T, SetValueFn<T>, GetValueFn<T>>

A nullable version of useGetState

Link copied to clipboard
fun <T> _useState(default: T): MutableState<T>

这是一个可空的useState,如果对象的状态可能为空,应该使用它。

Link copied to clipboard
fun <T> KFunction<T?>.asNoopFn(instance: Any? = null): (TParams) -> T?

用来将任意一个函数转换成 noop 函数。但是需要注意,如果这个函数是一个实例的函数,必须要传入对应的实例。

Link copied to clipboard
fun <T : Any> KFunction<T>.asSuspendNoopFn(instance: Any? = null): suspend (TParams) -> T

将一个 suspend 挂起函数转换成suspend 版本的 NormalFunction 函数。 直接使用 asNoopFn 会抛出异常,因为挂起函数的参数比参数列表还多一个挂起相关标识。

Link copied to clipboard
fun <T> createContext(initialValue: T): ReactContext<T>

function to create a context object.

Link copied to clipboard
operator fun SetValueFn<SetterEither<Int>>.invoke(rightValue: (Int) -> Int)
operator fun SetValueFn<SetterEither<Int>>.invoke(leftValue: Int)
operator fun VoidFunction.invoke(vararg params: Any?)

operator fun <TData> NormalFunction<TData>.invoke(vararg params: Any?): TData

优化函数调用形式,将导出的函数伪装成普通函数的样子,无需对参数进行arrayOf,可能需要手动导包:

Link copied to clipboard
fun LaunchedDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { DebounceOptions() }, block: SuspendAsyncFn)
Link copied to clipboard
fun LaunchedThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { ThrottleOptions() }, block: SuspendAsyncFn)
Link copied to clipboard

you should call this function in your PersistentSave fun to notify state update

Link copied to clipboard

Observe Ref as State

Link copied to clipboard
inline fun <T> optionsOf(noinline opt: T.() -> Unit): T

这个函数已经废弃,请使用性能优化后的 optionsOf = {} 来传递选项配置,这个函数将于不久后删除!

Link copied to clipboard
fun rememberAsync(): AsyncRunFn
fun rememberAsync(fn: SuspendAsyncFn): () -> Unit
Link copied to clipboard
fun <T> rememberAutoReset(default: T & Any, interval: Duration): MutableState<T & Any>
Link copied to clipboard
fun rememberBackToFrontEffect(vararg keys: Any?, effect: () -> Unit)
Link copied to clipboard

更符合 Compose 的函数命名方式

Link copied to clipboard
fun rememberBoolean(default: Boolean = false): Tuple5<Boolean, () -> Unit, (Boolean) -> Unit, () -> Unit, () -> Unit>
Link copied to clipboard
Link copied to clipboard
fun rememberClipboard(): Pair<CopyFn, PasteFn>
Link copied to clipboard
Link copied to clipboard
fun rememberCounter(initialValue: Int = 0, options: CounterOptions): Tuple5<Int, IncFn, DecFn, SetValueFn<Either<Int, (Int) -> Int>>, ResetFn>
Link copied to clipboard
fun <T> rememberCreation(vararg keys: Any?, factory: () -> T): Ref<T>
Link copied to clipboard
fun <S> rememberDebounce(value: S, options: DebounceOptions = remember { DebounceOptions() }): S
Link copied to clipboard
fun rememberDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { DebounceOptions() }): (TParams) -> Unit
Link copied to clipboard
fun rememberDisableScreenshot(): Tuple3<DisableFn, EnableFn, IsDisabled>
Link copied to clipboard
inline fun <A> rememberDispatch(alias: String? = null): Dispatch<A>

更符合 Compose 的函数命名方式

Link copied to clipboard
inline fun <A> rememberDispatchAsync(alias: String? = null, noinline onBefore: DispatchCallback<A>? = null, noinline onFinally: DispatchCallback<A>? = null): DispatchAsync<A>
Link copied to clipboard
Link copied to clipboard
inline fun <T : Any> rememberEventPublish(): (T) -> Unit
Link copied to clipboard
inline fun <T : Any> rememberEventSubscribe(noinline subscriber: (T) -> Unit)
Link copied to clipboard
fun rememberFlashlight(): Tuple2<TurnOnFn, TurnOffFn>
Link copied to clipboard
Link copied to clipboard
fun rememberFrontToBackEffect(vararg keys: Any?, effect: () -> Unit)
Link copied to clipboard
fun <T> rememberGetState(default: T & Any): Triple<T & Any, SetValueFn<T & Any>, GetValueFn<T & Any>>
Link copied to clipboard
Link copied to clipboard
fun rememberInterval(options: IntervalOptions = remember { IntervalOptions() }, block: () -> Unit): Triple<ResumeFn, PauseFn, IsActive>
fun rememberInterval(options: IntervalOptions = remember { IntervalOptions() }, ready: Boolean, block: () -> Unit)
Link copied to clipboard
fun rememberKeyboard(): Pair<HideKeyboardFn, ShowKeyboardFn>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun rememberMount(fn: SuspendAsyncFn)
Link copied to clipboard
fun rememberNow(options: UseNowOptions = remember { UseNowOptions() }): String
Link copied to clipboard
fun <T> rememberPersistent(key: String, defaultValue: T): Triple<T, SaveToPersistent<T>, PersistentClear>
Link copied to clipboard
fun <T> rememberPrevious(present: T): T?
Link copied to clipboard
fun <S, A> rememberReducer(reducer: Reducer<S, A>, initialState: S, middlewares: Array<Middleware<S, A>> = emptyArray()): Triple<S, Dispatch<A>, DispatchAsync<A>>
Link copied to clipboard
fun <T> rememberRef(default: T): MutableRef<T>
Link copied to clipboard
fun <TData : Any> rememberRequest(requestFn: SuspendNormalFunction<TData>, options: RequestOptions<TData> = RequestOptions(), plugins: Array<@Composable (RequestOptions<TData>) -> Plugin<TData>> = emptyArray()): Tuple7<TData?, Boolean, Throwable?, (TParams) -> Unit, ((TData?) -> TData) -> Unit, () -> Unit, () -> Unit>
Link copied to clipboard
fun <T> rememberResetState(default: T & Any): Tuple4<T, SetValueFn<T & Any>, GetValueFn<T>, ResetFn>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
inline fun <T> rememberSelector(alias: String? = null): T
inline fun <T, R> rememberSelector(alias: String? = null, block: T.() -> R): R
Link copied to clipboard
fun rememberSensor(sensorType: Int, onAccuracyChangedFn: (Sensor, Int) -> Unit = { _, _ -> }, onSensorChangedFn: (SensorEvent) -> Unit = { _ -> })
Link copied to clipboard
Link copied to clipboard
fun <S> rememberThrottle(value: S, options: ThrottleOptions = remember { ThrottleOptions() }): S
Link copied to clipboard
fun rememberThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { ThrottleOptions() }): (TParams) -> Unit
Link copied to clipboard
fun rememberTimeout(delay: Duration = 1.seconds, block: () -> Unit)
Link copied to clipboard
fun rememberTimestamp(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true): Tuple4<Long, () -> Unit, () -> Unit, IsActive>
Link copied to clipboard
fun rememberTimestampRef(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true): Tuple4<Ref<Long>, () -> Unit, () -> Unit, IsActive>
Link copied to clipboard
fun <T> rememberToggle(defaultValue: T? = null, reverseValue: T? = null): Pair<T?, ToggleFn>
Link copied to clipboard
fun <L, R> rememberToggleEither(defaultValue: L? = null, reverseValue: R? = null): Pair<Either<L?, R?>, ToggleFn>
Link copied to clipboard
fun rememberToggleVisible(isVisible: Boolean = false, content: @Composable () -> Unit): Pair<ComposeComponent, ToggleFn>
fun rememberToggleVisible(isFirst: Boolean = true, content1: @Composable () -> Unit, content2: @Composable () -> Unit): Pair<ComposeComponent, ToggleFn>
Link copied to clipboard
fun <T> rememberUndo(initialPresent: T): Tuple7<UndoState<T>, (T) -> Unit, (T) -> Unit, () -> Unit, () -> Unit, CanUndo, CanRedo>
Link copied to clipboard
fun rememberUnmount(block: () -> Unit)
Link copied to clipboard
Link copied to clipboard
fun rememberUpdateEffect(vararg keys: Any?, block: SuspendAsyncFn)
Link copied to clipboard
fun rememberVibrate(): Pair<() -> Unit, () -> Unit>
Link copied to clipboard
fun rememberWakeLock(): Tuple3<RequestFn, ReleaseFn, IsActive>
Link copied to clipboard
fun rememberWindowFlags(key: String, flags: Int): Tuple3<AddFlagsFn, ClearFlagsFn, IsFlagsAdded>
Link copied to clipboard
fun useAsync(): AsyncRunFn

This is a hook function that simplifies the use of coroutine scope. It's usage similar to the run function. Equivalent to scope.launch { }

fun useAsync(fn: SuspendAsyncFn): () -> Unit

This hook function accepts a suspend function as a parameter and returns a function(use to execute suspend function). When you call the execution function, the suspend function will be executed within the coroutine scope of the current component.

Link copied to clipboard
fun <T> useAutoReset(default: T & Any, interval: Duration): MutableState<T & Any>
Link copied to clipboard
fun useBackToFrontEffect(vararg deps: Any?, effect: () -> Unit)

Description: 进入后台再次回到前台时执行Effect,不同于官方 API,只在再进入时才执行。 第一次渲染(不执行)-> 进入后台在返回(执行)。

Link copied to clipboard
fun useBiometric(options: BiometricOptions = remember { BiometricOptions() }): Tuple2<() -> Unit, Boolean>
Link copied to clipboard
fun useBoolean(default: Boolean = false): Tuple5<Boolean, ToggleFn, SetValueFn<Boolean>, SetTrueFn, SetFalseFn>
Link copied to clipboard
fun useClipboard(): Pair<CopyFn, PasteFn>

快捷的使用剪切板:复制、粘贴

Link copied to clipboard

A React-ish hook that returns the current value for that context.

Link copied to clipboard
fun useCounter(initialValue: Int = 0, optionsOf: CounterOptions.() -> Unit): Tuple5<Int, (Int) -> Unit, (Int) -> Unit, (Either<Int, (Int) -> Int>) -> Unit, () -> Unit>

fun useCounter(initialValue: Int = 0, options: CounterOptions): Tuple5<Int, IncFn, DecFn, SetValueFn<SetterEither<Int>>, ResetFn>

Use counter

Link copied to clipboard
fun <T> useCreation(vararg keys: Any?, factory: () -> T): Ref<T>

Description: useCreationuseRef 的替代品 。而相比于 useRef,你可以使用 useCreation 创建一些常量, 这些常量和 useRef 创建出来的 ref 有很多使用场景上的相似, 但对于复杂常量的创建,useRef 却容易出现潜在的性能隐患。

Link copied to clipboard
fun <S> useDebounce(value: S, optionsOf: DebounceOptions.() -> Unit): S
fun <S> useDebounce(value: S, options: DebounceOptions = remember { DebounceOptions() }): S
Link copied to clipboard
fun useDebounceEffect(vararg keys: Any?, optionsOf: DebounceOptions.() -> Unit, block: SuspendAsyncFn)
fun useDebounceEffect(vararg keys: Any?, options: DebounceOptions = remember { DebounceOptions() }, block: SuspendAsyncFn)
Link copied to clipboard
fun useDebounceFn(fn: VoidFunction, optionsOf: DebounceOptions.() -> Unit): VoidFunction

fun useDebounceFn(fn: VoidFunction, options: DebounceOptions = remember { DebounceOptions() }): VoidFunction

需要注意:Debounce 不返回计算结果,在 Compose 中我们无法使用 Debounce 透传出结算结果,应该使用状态,而非 Debounce 的返回值。 例如我们有一个计算函数,我们应该设置一个状态作为结果的保存。函数计算后的结果,通过调用对应的 setState(state:T) 函数来传递。保证结算结果(状态)与计算解耦。 这样我们的Debounce 就可以无缝接入。

Link copied to clipboard
fun useDisableScreenshot(): Tuple3<DisableFn, EnableFn, IsDisabled>
Link copied to clipboard
Link copied to clipboard
fun useEffect(vararg deps: Any?, block: SuspendAsyncFn)
Link copied to clipboard
inline fun <T : Any> useEventPublish(): (T) -> Unit

This hook returns a publish function, use that fun to post a event.

Link copied to clipboard
inline fun <T : Any> useEventSubscribe(noinline subscriber: (T) -> Unit)

Register a subscriber. Note that this subscription function will be removed from the subscription list after the component is uninstalled.

Link copied to clipboard
fun useFlashlight(): Tuple2<TurnOnFn, TurnOffFn>
Link copied to clipboard
Link copied to clipboard
fun useFrontToBackEffect(vararg deps: Any?, effect: () -> Unit)

Use front to back effect,contrary to the useBackToFrontEffect behavior, the effect is executed when the App enters the background

Link copied to clipboard
fun <T> useGetState(default: T & Any): Triple<T, SetValueFn<T & Any>, GetValueFn<T>>

Description: Using destructuring declarations on useState can cause closure problems. Using useLatestRef is a solution, but if you call the set function quickly(millisecond level), there will be a problem of state loss.

Link copied to clipboard
Link copied to clipboard
fun useInterval(optionsOf: IntervalOptions.() -> Unit, block: () -> Unit): Triple<ResumeFn, PauseFn, IsActive>
fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, block: () -> Unit): Triple<ResumeFn, PauseFn, IsActive>
fun useInterval(optionsOf: IntervalOptions.() -> Unit, ready: Boolean, block: () -> Unit)
fun useInterval(options: IntervalOptions = remember { IntervalOptions() }, ready: Boolean, block: () -> Unit)
Link copied to clipboard
fun useKeyboard(): Pair<HideKeyboardFn, ShowKeyboardFn>
Link copied to clipboard
fun <T> useLatestRef(value: T): Ref<T>
Link copied to clipboard

Description: Alias for rememberUpdatedState

Link copied to clipboard
fun <T> useList(vararg elements: T): SnapshotStateList<T>
Link copied to clipboard
Link copied to clipboard
fun <K, V> useMap(vararg pairs: Pair<K, V>): SnapshotStateMap<K, V>
Link copied to clipboard
fun useMount(fn: SuspendAsyncFn)
Link copied to clipboard
fun useNow(optionsOf: UseNowOptions.() -> Unit): String
fun useNow(options: UseNowOptions = remember { UseNowOptions() }): String
Link copied to clipboard
fun <T> usePersistent(key: String, defaultValue: T, forceUseMemory: Boolean = false): PersistentHookReturn<T>

Use persistent

Link copied to clipboard
fun <T> usePrevious(present: T): T?
Link copied to clipboard
fun <S, A> useReducer(reducer: Reducer<S, A>, initialState: S, middlewares: Array<Middleware<S, A>> = emptyArray()): Triple<S, Dispatch<A>, DispatchAsync<A>>
Link copied to clipboard
fun <T> useRef(default: T): MutableRef<T>
Link copied to clipboard
fun <T> useResetState(default: T & Any): Tuple4<T, SetValueFn<T & Any>, GetValueFn<T>, ResetFn>
Link copied to clipboard
Link copied to clipboard
fun useSensor(sensorType: Int, onAccuracyChangedFn: (Sensor, Int) -> Unit = { _, _ -> }, onSensorChangedFn: (SensorEvent) -> Unit = { _ -> })
Link copied to clipboard
fun <T> useState(default: T & Any): MutableState<T & Any>

Description: 在compose中使用state需要写一点模板代码,虽然谈不上有多麻烦,但是还是能简化一下的; 返回值是MutableState,可以解构后使用。如果是可空对象需要显示声明:

fun <T> useState(vararg keys: Any, factory: () -> T): State<T>

用于方便的创建派生状态,派生状态不同于普通的MutableState,他是一个只读的状态, 它只会在依赖发生变化时重新计算改变状态,在其他的框架中也称之为计算属性或者计算状态。

Link copied to clipboard
fun <S> useThrottle(value: S, optionsOf: ThrottleOptions.() -> Unit): S
fun <S> useThrottle(value: S, options: ThrottleOptions = remember { ThrottleOptions() }): S
Link copied to clipboard
fun useThrottleEffect(vararg keys: Any?, optionsOf: ThrottleOptions.() -> Unit, block: SuspendAsyncFn)
fun useThrottleEffect(vararg keys: Any?, options: ThrottleOptions = remember { ThrottleOptions() }, block: SuspendAsyncFn)
Link copied to clipboard
fun useThrottleFn(fn: VoidFunction, optionsOf: ThrottleOptions.() -> Unit): VoidFunction
fun useThrottleFn(fn: VoidFunction, options: ThrottleOptions = remember { ThrottleOptions() }): VoidFunction
Link copied to clipboard
fun useTimeout(delay: Duration = 1.seconds, block: () -> Unit)
Link copied to clipboard
fun useTimestamp(optionsOf: TimestampOptions.() -> Unit, autoResume: Boolean = true): Tuple4<Long, PauseFn, ResumeFn, IsActive>

fun useTimestamp(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true): Tuple4<Long, PauseFn, ResumeFn, IsActive>

Use timestamp

Link copied to clipboard
fun useTimestampRef(optionsOf: TimestampOptions.() -> Unit, autoResume: Boolean = true): Tuple4<Ref<Long>, PauseFn, ResumeFn, IsActive>

fun useTimestampRef(options: TimestampOptions = remember { TimestampOptions() }, autoResume: Boolean = true): Tuple4<Ref<Long>, PauseFn, ResumeFn, IsActive>

Use timestamp ref

Link copied to clipboard
fun <T> useToggle(defaultValue: T? = null, reverseValue: T? = null): Pair<T?, ToggleFn>

用于在两个状态值间切换的 Hook。

Link copied to clipboard
fun <L, R> useToggleEither(defaultValue: L? = null, reverseValue: R? = null): Pair<Either<L?, R?>, ToggleFn>

Description:用于在两个状态值间切换的 Hook。为了保证类型信息不消失,使用Either作为容器保存; 调用者在使用时根据实际情况调用 Either.fold 函数或者其他函数处理;

Link copied to clipboard
fun useToggleVisible(isVisible: Boolean = false, content: ComposeComponent): Pair<ComposeComponent, ToggleFn>

用于方便的切换控制组件的可见性

fun useToggleVisible(isFirst: Boolean = true, content1: ComposeComponent, content2: ComposeComponent): Pair<ComposeComponent, ToggleFn>
Link copied to clipboard
fun <T> useUndo(initialPresent: T): Tuple7<UndoState<T>, SetValueFn<T>, ResetValueFn<T>, UndoFn, RedoFn, CanUndo, CanRedo>
Link copied to clipboard
fun useUnmount(block: () -> Unit)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun useUpdateEffect(vararg deps: Any?, block: SuspendAsyncFn)
Link copied to clipboard
fun useWakeLock(): Tuple3<RequestFn, ReleaseFn, IsActive>
Link copied to clipboard
fun useWindowFlags(key: String, flags: Int): Tuple3<AddFlagsFn, ClearFlagsFn, IsFlagsAdded>