use-observable-hooks
    Preparing search index...

    Function useSubject

    Implementation of useSubject.

    • Hook to use an RxJS Subject.

      Returns the current value, a function to emit a new value, and any error. A Subject is a special type of Observable that allows values to be multicasted to many Observers.

      Type Parameters

      • SubjectValue

        The type of the value emitted by the subject.

      • SubjectError = unknown

        The type of the error that can be emitted by the subject. Defaults to unknown.

      Returns [
          SubjectValue
          | undefined,
          (value: SubjectValue) => void,
          SubjectError | undefined,
      ]

      A tuple containing:

      • The latest value emitted by the subject (or undefined if none emitted yet, or the initial value for BehaviorSubject).
      • A function to emit a new value to the subject.
      • The error emitted by the subject (if any).
      import { useSubject } from 'use-observable-hooks';

      function EventEmitter() {
      const [event, emitEvent] = useSubject<string>();

      return (
      <div>
      <p>Last Event: {event}</p>
      <button onClick={() => emitEvent('Click')}>Emit Click</button>
      <button onClick={() => emitEvent('Hover')}>Emit Hover</button>
      </div>
      );
      }
    • Hook to use an RxJS Subject with pipe operations.

      Type Parameters

      • SubjectValue

        The type of the value emitted by the subject after piping.

      • SubjectError = unknown

        The type of the error that can be emitted by the subject. Defaults to unknown.

      Parameters

      • ...operations: OperatorFunction<any, any>[]

        Operator functions to pipe to the subject.

      Returns [SubjectValue | undefined, (value: any) => void, SubjectError | undefined]

      A tuple containing:

      • The latest value emitted by the subject (or undefined).
      • A function to emit a new value to the subject.
      • The error emitted by the subject (if any).
      import { useSubject } from 'use-observable-hooks';
      import { debounceTime, map } from 'rxjs/operators';

      function SearchInput() {
      const [debouncedSearch, setSearch] = useSubject<string>(
      debounceTime(300),
      map((query) => query.toLowerCase())
      );

      return (
      <div>
      <input onChange={(e) => setSearch(e.target.value)} />
      <p>Searching for: {debouncedSearch ?? 'Nothing yet'}</p>
      </div>
      );
      }