crimx / observable-hooks

⚛️☯️💪 React hooks for RxJS Observables. Concurrent mode safe.
https://observable-hooks.js.org
MIT License
1.03k stars 44 forks source link

useObservableEagerState sometimes calls emitted functions #131

Closed robintown closed 4 months ago

robintown commented 4 months ago

Let myObservable: Observable<() => string> = of(() => "hello"). If you try to consume this observable from React using useObservableEagerState, you would expect to get a value of type () => string, but sometimes you actually get a string. Here's a full example:

const myObservable = of(() => "hello")

const MyComponent: FC = () => {
  const value = useObservableEagerState(myObservable)
  useEffect(() => console.log(`value is ${value}`), [value])
  return null
}

Expected result: When MyComponent is mounted, value is function value() is logged to the console. Actual result: value is hello is sometimes logged to the console.

I suspect this is due to useState accepting a function, which it will call to determine its initial value.