Skip to content
Compare
Choose a tag to compare
@github-actions github-actions released this 24 Feb 14:52
80db725

Minor Changes

  • #5200 0332a16a42fb372eb614df74ff4cb7f003c31fc8 Thanks @{! - Added selectors to @xstate/store that enable efficient state selection and subscription:

    • store.select(selector) function to create a "selector" entity where you can:
      • Get current value with .get()
      • Subscribe to changes with .subscribe(callback)
      • Only notify subscribers when selected value actually changes
      • Support custom equality functions for fine-grained control over updates via store.select(selector, equalityFn)
    const store = createStore({
      context: {
        position: { x: 0, y: 0 },
     name: 'John', age: 30 }
      },
      on: {
        positionUpdated: (
          context,
          event: { position: { x: number; y: number } }
        ) => ({
          ...context,
          position: event.position
        })
      }
    });
    
    const position = store.select((state) => state.context.position);
    
    position.get(); // { x: 0, y: 0 }
    
    position.subscribe((position) => {
      console.log(position);
    });
    
    store.trigger.positionUpdated({ x: 100, y: 200 });
    // Logs: { x: 100, y: 200 }