Skip to content

How to correctly implement Checkboxes and Radios? #134

Answered by ivan-dalmet
MattHeslington asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @MattHeslington
Thanks for the kind word.

You can pass a defaultValue to false if no defaultValue is provided

const Checkbox = (props) => {
    const { errorMessage, isValid, value, setValue } = useField({ 
      ...props, 
      defaultValue: props.defaultValue ?? false  // Update here
    });
    const { title, required, name, style, ...otherProps } = props
    const showError = !isValid

    return (
        <div>
            <input
                id={name}
                type='checkbox'
                checked={value} 
                required={required}
                onClick={() => setValue(!value)}
                {...otherProps}
            />
            <label htmlFor={name}>

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@MattHeslington
Comment options

Answer selected by MattHeslington
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants