Skip to content

Instantly share code, notes, and snippets.

@asaumet230
Last active April 15, 2022 15:23
Show Gist options
  • Save asaumet230/c4a3d0649c67e180c8bbc0a2d9c4fd7e to your computer and use it in GitHub Desktop.
Save asaumet230/c4a3d0649c67e180c8bbc0a2d9c4fd7e to your computer and use it in GitHub Desktop.
Typescript: reducer
import { useReducer, useEffect } from 'react';
interface AuthState {
validando: boolean;
token: null | string;
username: string;
nombre: string;
}
type LoginPayload = {
username: string,
nombre: string
}
type AuthAction =
| { type: 'logout' }
| { type: 'login', payload: LoginPayload }
const initialState: AuthState = {
validando: true,
token: null,
username: '',
nombre: ''
}
const authReducer = (state: AuthState, action: AuthAction): AuthState => {
switch (action.type) {
case 'logout':
return {
validando: false,
token: null,
username: '',
nombre: ''
}
case 'login':
return {
validando: false,
token: 'ABC123',
username: action.payload.username,
nombre: action.payload.nombre
}
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment