Using xstate, is it possible to configure an event that is applicable under all states and is handled in the same way across all states and substates?

末鹿安然 提交于 2020-12-13 03:35:36

问题


I am new to xstate, and I'm trying to use it in an application where a user can request different things in an application, based on parent state and/or sub-state. However, there are some requests that the user should be able to make, no matter what state/sub-state the app is in. The response to those events is the same, no matter what the previous state was. How can I configure this event, so that I don't have to repeat define it under all states/sub-states?


回答1:


Yes - the algorithm for choosing transitions is similar to DOM event propagation, in that it searches from leaf nodes to root node.

You can define transitions on the root node (top-level) which will be handled in any state naturally:

import { createMachine } from 'xstate';

const machine = createMachine({
  // ...

  // top-level transitions
  on: {
    ESC: {/* ... */}
  },
  states: {
    // ...
    someState: {
      on: {
        ESC: {/* override top-level transition */}
      }
    }
  }
});


来源:https://stackoverflow.com/questions/61393903/using-xstate-is-it-possible-to-configure-an-event-that-is-applicable-under-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!