Skip to content

Commit

Permalink
useRxRefPropValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed May 29, 2024
1 parent 425fe8b commit c1f8e85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-drinks-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx-react": patch
---

useRxRefPropValue
16 changes: 11 additions & 5 deletions packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,21 @@ export const useRxSubscribe = <A>(
* @category hooks
*/
export const useRxRef = <A>(ref: RxRef.ReadonlyRef<A>): A => {
const [value, setValue] = React.useState(ref.value)
const [, setValue] = React.useState(ref.value)
React.useEffect(() => ref.subscribe(setValue), [ref])
return value
return ref.value
}

/**
* @since 1.0.0
* @category hooks
*/
export const useRxRefProp = <A, K extends keyof A>(ref: RxRef.RxRef<A>, prop: K): RxRef.RxRef<A[K]> => {
return React.useMemo(() => ref.prop(prop), [ref, prop])
}
export const useRxRefProp = <A, K extends keyof A>(ref: RxRef.RxRef<A>, prop: K): RxRef.RxRef<A[K]> =>
React.useMemo(() => ref.prop(prop), [ref, prop])

/**
* @since 1.0.0
* @category hooks
*/
export const useRxRefPropValue = <A, K extends keyof A>(ref: RxRef.RxRef<A>, prop: K): A[K] =>
useRxRef(useRxRefProp(ref, prop))

0 comments on commit c1f8e85

Please sign in to comment.