Sitemap
WellD Tech

We write about our efforts to keep a low rate of WTF per minute. (┛◉Д◉)┛彡┻━┻

Follow publication

Untangle complex flows in your React Native app with XState

5 min readJan 14, 2021

--

A storyboard for the payment authorisation flow
A storyboard for the payment authorisation flow

Decoupling screens and business logic

The finite state machine for the payment authorisation flow.
The finite state machine for the payment authorisation flow. Notice how states and screens do not have a 1:1 correspondence!
.

Modelling with XState

// in the machine config
checkPrerequisites: {
invoke: {
id: 'checkPrerequistes',
src: 'checkPrerequisites',
onDone: {
target: 'fetchingPaymentDetails',
},
onError: {
target: 'prerequisitesNotMet',
},
},
}
// in the machine options
services: {
// we return a Promise here
checkPrerequisites: () => Biometrics.isEnrolled(),
// ...
}
fetchingPaymentDetails: {
invoke: {
id: 'fetchPaymentDetails',
src: 'fetchPaymentDetails',
onDone: {
target: 'paymentDetailsFetched',
// fetchPaymentDetails resolves to
// { paymentAuthorizationId, expiresOn }
actions: assign((ctx, event) => {
return event.data;
}),
},
onError: {
target: 'paymentDetailsError',
},
},
}
// in the machine config
paymentDetailsFetched: {
after: {
AUTHORIZATION_EXPIRES: {
target: 'authorizationExpired',
},
},
// ...
}
// in the machine options
delays: {
AUTHORIZATION_EXPIRES: (ctx) => {
return Math.max(ctx.expiresOn - new Date(), 0);
},
// ...
}

Phew, that’s a wrap for now!

Further reading

Notes

--

--

WellD Tech
WellD Tech

Published in WellD Tech

We write about our efforts to keep a low rate of WTF per minute. (┛◉Д◉)┛彡┻━┻

Simone D'Avico
Simone D'Avico

Written by Simone D'Avico

Software Engineer with a passion for programming languages

Responses (2)