Appearance
useProjection
Category
Export Size
142 B
Package
@reause/math Last Changed
5 days ago
Related
Reactive numeric projection from one domain to another
Demo
Usage
tsx
import { useProjection } from '@reause/math'
import { useState } from 'react'
const [input, setInput] = useState(0)
const projected = useProjection(input, [0, 10], [0, 100])
setInput(5) // projected === 50 on the next render
setInput(10) // projected === 100 on the next renderinput, fromDomain and toDomain are plain read-only values (upstream takes MaybeRefOrGetter<...>). Re-render with new values — e.g. from useState — and the hook recomputes:
tsx
import { useProjection } from '@reause/math'
import { useState } from 'react'
const [from, setFrom] = useState<readonly [number, number]>([0, 10])
const projected = useProjection(5, from, [0, 100])
setFrom([0, 20]) // triggers a re-renderType Declarations
ts
export type ProjectorFunction<F, T> = (input: F, from: readonly [
F,
F
], to: readonly [
T,
T
]) => T
export function useProjection(input: number, fromDomain: readonly [
number,
number
], toDomain: readonly [
number,
number
], projector?: ProjectorFunction<number, number>): number