github statelyai/xstate @xstate/react@1.5.0

2 years ago

Minor Changes

  • 432b60f7 #2280 Thanks @davidkpiano! - Just like useInterpret(...), other types of actors can now be spawned from behaviors using useSpawn(...):

    import { fromReducer } from 'xstate/lib/behaviors';
    import { useActor, useSpawn } from '@xstate/react';
    
    type CountEvent = { type: 'INC' } | { type: 'DEC' };
    
    const countBehavior = fromReducer(
      (count: number, event: CountEvent): number => {
        if (event.type === 'INC') {
          return count + 1;
        } else if (event.type === 'DEC') {
          return count - 1;
        }
    
        return count;
      },
      0 // initial state
    );
    
    const Component = () => {
      const countActorRef = useSpawn(countBehavior);
      const [count, send] = useActor(countActorRef);
    
      return (
        <div>
          Count: {count}
          <button onClick={() => send({ type: 'INC' })}>Increment</button>
          <button onClick={() => send({ type: 'DEC' })}>Decrement</button>
        </div>
      );
    };

Don't miss a new xstate release

NewReleases is sending notifications on new releases.