import { FC, useCallback, useReducer } from "react"; import ReactDOM from "react-dom/client"; import "./index.css"; import reportWebVitals from "./reportWebVitals"; enum ActionType { SET_NAME, INCREMENT_SALARY, FIRE, } type NumberAction = { type: TAction; payload: number; }; type EmptyAction = { type: TAction; }; type Action = | NumberAction | EmptyAction; type State = { salary: number; fired: boolean; name: string; }; const stateReducer = (state: State, action: Action): State => { switch (action.type) { case ActionType.FIRE: return { ...state, salary: 0, fired: true }; case ActionType.INCREMENT_SALARY: return { ...state, salary: state.salary + action.payload }; } }; const defaultState = { name: "Super Employee", salary: 100, fired: false }; const App = () => { const [state, dispatch] = useReducer(stateReducer, defaultState); const { name, salary, fired } = state; const fire = useCallback(() => dispatch({ type: ActionType.FIRE }), []); const incrementSalary = useCallback( () => dispatch({ type: ActionType.INCREMENT_SALARY, payload: 50 }), [] ); return fired ? ( ) : ( ); }; const Fired: FC<{ employeeName: string }> = ({ employeeName }) => (
{employeeName} is fired
); type EmployeeInterfaceProps = { employeeName: string; salary: number; onSalaryIncrement: () => void; onFired: () => void; }; const EmployeeInterface: FC = ({ employeeName, salary, onFired, onSalaryIncrement, }) => { return (

Employee: {employeeName}

Salary: {salary}
); }; ReactDOM.createRoot(document.getElementById("root")!).render(); reportWebVitals();