AppleSauce
    Preparing search index...

    Function useObservableState

    • A sugar hook for getting values from an Observable.

      It can be used in two ways:

      1. Offer an Observable and an optional initial state.
        const output = useObservableState(input$, initialState)
        
      2. Offer an epic-like function and an optional initial state.
        const [output, onInput] = useObservableState(
        (input$, initialState) => input$.pipe(...),
        initialState
        )

      The optional initialState is internally passed to useState(initialState). This means it can be either a state value or a function that returns the state which is for expensive initialization.

      The initialState(or its returned result) is also passed to the init function. This is useful if you want to implement reducer pattern which requires an initial state.

      Note: These two ways use different hooks, choose either one each time and do not change to the other one during Component's life cycle.

      Note: useObservableState will call the epic-like init function only once and always return the same Observable. It is not safe to access closure directly inside init. Use [[useObservable]] with withLatestFrom instead.

      Note: To make it concurrent mode compatible, the subscription happens after the render is committed to the screen which means even the Observable emits synchronous values they will arrive after the first rendering.

      Type Parameters

      • TState

        Output state.

      Parameters

      • input$: BehaviorSubject<TState>

        A BehaviorSubject.

      Returns TState

    • Type Parameters

      • TState

        Output state.

      Parameters

      • input$: Observable<TState>

        An Observable.

      Returns undefined | TState

    • Type Parameters

      • TState

        Output state.

      Parameters

      • input$: Observable<TState>

        An Observable.

      • initialState: TState | (() => TState)

        Optional initial state. Can be the state value or a function that returns the state.

      Returns TState

    • Type Parameters

      • TState

        Output state.

      • TInput = TState

        Input values.

      Parameters

      • init: (input$: Observable<TInput>) => Observable<TState>

        A epic-like function that, when applied to an Observable and the initial state value, returns an Observable.

      Returns [undefined | TState, (input: TInput) => void]

    • Different input output types with initial state.

      Type Parameters

      • TState

        Output state.

      • TInput = TState

        Input values.

      Parameters

      • init: (input$: Observable<TInput>, initialState: TState) => Observable<TState>

        A epic-like function that, when applied to an Observable and the initial state value, returns an Observable.

      • initialState: TState | (() => TState)

        Optional initial state. Can be the state value or a function that returns the state.

      Returns [TState, (input: TInput) => void]