Created
September 8, 2022 09:43
-
-
Save a-eid/5a2e6a1598b66f40374933de15dd454b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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