# 副作用

¥Effects

这些 XState v4 文档不再维护

XState v5 现已推出!阅读有关 XState v5 的更多信息 (opens new window)

¥XState v5 is out now! Read more about XState v5 (opens new window)

🆕 在我们的新文档中查找有关 XState 中的副作用和操作 (opens new window) 的更多信息。

¥🆕 Find more about effects and actions in XState (opens new window) in our new docs.

在状态图中,"side-effects" 可以分为两类:

¥In statecharts, "side-effects" can be grouped into two categories:

"一劳永逸" 效果,执行同步副作用,没有事件发送回状态图,或同步发送事件回状态图:

¥**"Fire-and-forget" effects**, which execute a synchronous side-effect with no events sent back to the statechart, or send an event synchronously back to the statechart:

  • 行动 - 单一、离散效应

    ¥Actions - single, discrete effects

  • 活动 - 当它们开始时的状态退出时处理的连续副作用

    ¥Activities - continuous effects that are disposed when the state they were started in is exited

调用的效果,它执行可以异步发送和接收事件的副作用:

¥Invoked effects, which execute a side-effect that can send and receive events asynchronously:

  • 调用的 Promise - 随着时间的推移,可能会产生 resolvereject 次的单一离散效应,这些效应会作为事件发送到父计算机

    ¥Invoked Promises - single, discrete effects over time that may resolve or reject once, which are sent as events to the parent machine

  • 调用的回调 - 随着时间的推移,可能会发送多个事件,以及监听直接发送到它、发送到/来自父计算机的事件的连续副作用

    ¥Invoked Callbacks - continuous effects over time that may send multiple events, as well as listen for events sent directly to it, to/from the parent machine

  • 调用的可观察量 - 随着时间的推移,连续的影响可能会发送由来自观察流的消息触发的多个事件

    ¥Invoked Observables - continuous effects over time that may send multiple events triggered by messages from the observed stream

  • 调用机器 - 连续副作用由 Machine 实例表示,可以发送/接收事件,但也可以在到达 最终状态 时通知父计算机

    ¥Invoked Machines - continuous effects represented by Machine instances that can send/receive events, but also notify the parent machine when it has reached its final state