Explicit states
export type State =
| {
state: 'UNAUTHENTICATED'
}
| {
state: 'AUTHENTICATING'
}
| {
state: 'AUTHENTICATED'
user: { name: string }
}
| {
state: 'ERROR'
error: string
}import { createReducer, States } from 'react-states'
export type State =
| {
state: 'UNAUTHENTICATED'
}
| {
state: 'AUTHENTICATING'
}
| {
state: 'AUTHENTICATED'
user: { name: string }
}
| {
state: 'ERROR'
error: string
}
export type PublicAction = {
type: 'AUTHENTICATE'
}
export type Feature = States<State, PublicAction>
const reducer = createReducer<Feature>({
UNAUTHENTICATED: {},
AUTHENTICATING: {},
AUTHENTICATED: {},
ERROR: {}
})
export const FeatureProvider = () => {
const feature = useReducer(reducer, {
state: 'UNAUTHENTICATED'
})
...
}Base state
Nested states
Last updated
Was this helpful?