diff --git a/.changeset/unlucky-schools-arrive.md b/.changeset/unlucky-schools-arrive.md new file mode 100644 index 0000000000..fe869fbd30 --- /dev/null +++ b/.changeset/unlucky-schools-arrive.md @@ -0,0 +1,5 @@ +--- +'@xstate/react': patch +--- + +Fixed an issue with `toObserver` being internally imported from `xstate/lib/utils` which has broken UMD build and the declared peer dep contract. diff --git a/packages/xstate-react/src/useInterpret.ts b/packages/xstate-react/src/useInterpret.ts index 4984065358..3888d0da68 100644 --- a/packages/xstate-react/src/useInterpret.ts +++ b/packages/xstate-react/src/useInterpret.ts @@ -11,12 +11,31 @@ import { Typestate, Observer } from 'xstate'; -import { toObserver } from 'xstate/lib/utils'; import { MaybeLazy } from './types'; import useConstant from './useConstant'; import { UseMachineOptions } from './useMachine'; import { useReactEffectActions } from './useReactEffectActions'; +// copied from core/src/utils.ts +// it avoids a breaking change between this package and XState which is its peer dep +function toObserver( + nextHandler: Observer | ((value: T) => void), + errorHandler?: (error: any) => void, + completionHandler?: () => void +): Observer { + if (typeof nextHandler === 'object') { + return nextHandler; + } + + const noop = () => void 0; + + return { + next: nextHandler, + error: errorHandler || noop, + complete: completionHandler || noop + }; +} + export function useInterpret< TContext, TEvent extends EventObject,