仅在条件通过时设置值的瞬态转换
Transient transition that sets a value only on condition passing
取以下代码:
const isWarning = () => { ... }
const setWarning = () => { ... }
const machine = Machine({
initial: "foo",
context: {
warning: null
},
states: {
foo: {
on: {
"": [
target: "bar",
action: "setWarning",
cond: "isWarning",
]
}
},
bar: {
on: {
FOO: "foo,
}
}
}
}, {
actions: {
setWarning
}
guards: {
isWarning
}
});
这是去"bar"和根据"foo"中的一些定量数据设置警告的最佳方式吗?
根据发布的代码示例,我不确定您所说的 "quantitative data in foo" 是什么意思。与机器行为相关的数据可以存储在机器的 context
或状态的 meta
属性 中。
要进入 bar
状态 和 设置警告,您可能需要类似以下内容:
const sm = Machine({
initial: 'baz',
context: { wasWarned: false },
on: {
'WARNING': {
target: 'bar',
action: 'setWarning'
}
},
states: {
baz: {},
bar: {}
}
}, {
actions: {
setWarning: assign({ warning: true })
}
})
这意味着:当机器收到 'WARNING' 事件时,进入 bar
状态并立即 before anything else 更新上下文。
Actions are not immediately triggered. Instead, the State object returned from machine.transition(...) will declaratively provide an array of .actions
that an interpreter can then execute.
transition will be enabled守卫通过后。
其他可能有用的代码示例,具体取决于您要实现的目标:
const sm = Machine({
initial: 'pending',
context: { wasWarned: null },
states: {
pending: {
on: {
'': [
{target: 'bar', cond:'wasWarned'},
{target: 'baz', cond: 'otherCond'}
]
}
},
bar: {},
baz: {}
},
guards: {
wasWarned: (ctx) => ctx.wasWarned
}
})
取以下代码:
const isWarning = () => { ... }
const setWarning = () => { ... }
const machine = Machine({
initial: "foo",
context: {
warning: null
},
states: {
foo: {
on: {
"": [
target: "bar",
action: "setWarning",
cond: "isWarning",
]
}
},
bar: {
on: {
FOO: "foo,
}
}
}
}, {
actions: {
setWarning
}
guards: {
isWarning
}
});
这是去"bar"和根据"foo"中的一些定量数据设置警告的最佳方式吗?
根据发布的代码示例,我不确定您所说的 "quantitative data in foo" 是什么意思。与机器行为相关的数据可以存储在机器的 context
或状态的 meta
属性 中。
要进入 bar
状态 和 设置警告,您可能需要类似以下内容:
const sm = Machine({
initial: 'baz',
context: { wasWarned: false },
on: {
'WARNING': {
target: 'bar',
action: 'setWarning'
}
},
states: {
baz: {},
bar: {}
}
}, {
actions: {
setWarning: assign({ warning: true })
}
})
这意味着:当机器收到 'WARNING' 事件时,进入 bar
状态并立即 before anything else 更新上下文。
Actions are not immediately triggered. Instead, the State object returned from machine.transition(...) will declaratively provide an array of
.actions
that an interpreter can then execute.
transition will be enabled守卫通过后。
其他可能有用的代码示例,具体取决于您要实现的目标:
const sm = Machine({
initial: 'pending',
context: { wasWarned: null },
states: {
pending: {
on: {
'': [
{target: 'bar', cond:'wasWarned'},
{target: 'baz', cond: 'otherCond'}
]
}
},
bar: {},
baz: {}
},
guards: {
wasWarned: (ctx) => ctx.wasWarned
}
})