Skip to content

Instantly share code, notes, and snippets.

@a-eid
Created September 8, 2022 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-eid/5a2e6a1598b66f40374933de15dd454b to your computer and use it in GitHub Desktop.
Save a-eid/5a2e6a1598b66f40374933de15dd454b to your computer and use it in GitHub Desktop.
import {useComputed, Signal} from '@preact/signals-react'
import {ReactNode} from 'react'
type SignalConsumerProps<Signal, Selection> = {
signal: Signal
children: (value: Selection) => ReactNode
selector: (s: Signal) => Selection
}
export function ComputedSignalConsumer<Signal, Selection>({
signal,
children,
selector,
}: SignalConsumerProps<Signal, Selection>) {
const selection = useComputed(() => selector(signal))
return children(selection.value) as JSX.Element
}
type ConsumerProps<Value> = {
children: (value: Value) => ReactNode
signal: Signal<Value>
}
export function SignalConsumer<Value>({children, signal}: ConsumerProps<Value>) {
return children(signal.value) as JSX.Element
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment