Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useObservableState(BehaviorSubject) gives undefined when first rendering #88

Closed
NoMoreViolence opened this issue Nov 10, 2021 · 2 comments

Comments

@NoMoreViolence
Copy link

NoMoreViolence commented Nov 10, 2021

The title says it all

import { BehaviorSubject } from "rxjs";
import { useObservableState } from "observable-hooks";

const tempBehavior$ = new BehaviorSubject({ hello: "message" });

export default function App() {
  const tempBehavior = useObservableState(tempBehavior$);

  return <div>{tempBehavior.hello}</div>;
}

Test Link is here: https://codesandbox.io/s/aged-butterfly-rqzwb?file=/src/App.tsx

the typing about this form should be like that

export function useObservableState<TState>(
  input$: BehaviorSubject<TState>
): TState

rxjs version: 6, 7 (both not working)

@Paitum
Copy link

Paitum commented Nov 10, 2021

If you want to have the initial value synchronously extracted from the BehaviorSubject then you should consider using useObservableEagerState https://observable-hooks.js.org/api/#useobservableeagerstate .

Alternatively, you could provide the useObservableState an "initial value" as its second argument. Like:

  const tempBehavior = useObservableState(tempBehavior$, {});

@crimx
Copy link
Owner

crimx commented Nov 10, 2021

This is actually a bug. useObservableState should pick up the BehaviorSubject.value automatically. I am fixing the tests to reflect that.

@crimx crimx closed this as completed in 7603979 Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants