useSpawn.js 492 B

123456789101112131415
  1. import { spawnBehavior } from 'xstate';
  2. import useConstant from './useConstant';
  3. /**
  4. * React hook that spawns an `ActorRef` with the specified `behavior`.
  5. * The returned `ActorRef` can be used with the `useActor(actorRef)` hook.
  6. *
  7. * @param behavior The actor behavior to spawn
  8. * @returns An ActorRef with the specified `behavior`
  9. */
  10. export function useSpawn(behavior) {
  11. var actorRef = useConstant(function () {
  12. return spawnBehavior(behavior);
  13. });
  14. return actorRef;
  15. }