Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.09 KB

useStateWithValidation.md

File metadata and controls

44 lines (33 loc) · 1.09 KB

useStateWithValidation

A hook to manage state with validation, providing validity status.

Arguments

  • validationFunc (function): The function to validate the state.
  • initialValue (any): The initial value for the state.

Returns

  • [state, onChange, isValid] (array):
    • state (any): The current state.
    • onChange (function): Function to update the state.
    • isValid (boolean): Validation status of the current state.

Hooks Involved

How to Use

import useStateWithValidation from "./useStateWithValidation"

export default function StateWithValidationComponent() {
    const [username, setUsername, isValid] = useStateWithValidation(
        name => name.length > 5,
        ""
    )

    return (
        <>
            <div>Valid: {isValid.toString()}</div>
            <input
                type="text"
                value={username}
                onChange={e => setUsername(e.target.value)}
            />
        </>
    )
}