我有一个多步骤表格,基本上有这些基本步骤:select services -> contact -> billing
. 当用户更改他们所在的步骤时,我会显示一个进度条并发出事件,这是我当前使用 xstate 的基本模式:
const formMachine = new Machine({
id: 'form-machine',
initial: 'selectService',
context: {
progress: 0,
pathname: '/select-service',
},
states: {
selectService: {
entry: assign({
progress: 0,
pathname: '/select-service',
}),
on: {
NEXT: 'contact',
}
},
contact: {
entry: assign({
progress: 1 / 3,
pathname: '/billing'
}),
on: {
PREVIOUS: 'selectService',
NEXT: 'billing',
}
},
// ... there's more actions but that's the gist of it
}
});
在我的反应组件中,我观察这个服务的变化,pathname
所以我可以推送到历史记录
function SignupFormWizard() {
const history = useHistory();
const [state, send, service] = useMachine(formMachine);
useEffect(() => {
const subscription = service.subscribe((state) => {
history.push(state.context.pathname);
});
return subscription.unsubscribe;
}, [service, history]);
// ...
}
但是,问题是:每当我重新访问一条路线(例如,我直接导航到/billing
)时,它会立即将我带回/select-service
. 这对我的代码来说是有意义的,因为初始状态和订阅会这样做。
我将如何在特定节点初始化状态机?