UNPKG

@undermuz/use-form

Version:
37 lines (36 loc) 869 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), // createReset(props), createSend(props) ], [] ); const [state, dispatch, { store, reset }] = useReducer( formReducer, initialState, middlewares, { debug: true } ); return { state, dispatch, store, reset }; }; export { useFormState };