UNPKG

@undermuz/use-form

Version:
33 lines (32 loc) 791 B
// src/useForm/useFormState.ts import { formReducer } from "./reducer.js"; import { useMemo } from "react"; import { getInitialState } from "./getInitialState.js"; import { createSend } from "./middlewares/send.js"; import { createValidating } from "./middlewares/validate.js"; import { useReducer } from "../utils/useReducer.js"; var useFormState = (props) => { const initialState = useMemo( () => getInitialState(props), [] ); const middlewares = useMemo( () => [ ...(props == null ? void 0 : props.middlewares) || [], createValidating(props), createSend(props) ], [] ); const [state, dispatch, store] = useReducer( formReducer, initialState, middlewares ); return { state, dispatch, store }; }; export { useFormState };