formik: Checkbox doesn't bind to initialValues

🐛 Bug report

Current Behavior

When using <Field type="checkbox /> and initialValues the checkbox doesn’t bind.

Expected behavior

<Field type="checkbox" /> should bind to initialValues

Reproducible example

https://codesandbox.io/s/ppy7wyrl0m

Suggested solution(s)

See #1024

Additional context

See #1024

Your environment

Software Version(s)
Formik 1.3.1
React 16.5.2

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 25 (2 by maintainers)

Most upvoted comments

This workaround is a little simpler, adding the checked property on the Field (works since the props get spread onto the input)

<Field type="checkbox" name="box" checked={props.values.box} />

This is still an issue

I think this is still a problem.

In the following sandbox, the initialValues only works when <Field /> isn’t given type="checkbox".

https://codesandbox.io/s/p7zlvk86lm

This is pretty sad guys…

This worked as a workaround for me.

const CheckboxInput = (props) => (
	<Field {...props} render={({field}) => <input {...field} type="checkbox" checked={field.value} />} />
);

Please suggest work around for checkbox with Field render for formik 1.5.7

@prajavk , It works for me:

<Field type="checkbox" name="manual-only" id="manual-only" onChange={event => setFieldValue('manualOnly', event.target.checked)} checked={values.manualOnly} />

I have the same problem. Third formik bug I have run into since starting to use the library last week… faith has been shaken.

Not fixed yet

I was having this issue with a checkbox group, so I’m leaving this here in case someone else has the same problem and google brings them here. This also uses material ui:

export type CheckboxGroupOptions = { value: string; label: string }[];

export interface Props {
  field: FieldInputProps<string>;
  options: CheckboxGroupOptions;
}

const CheckboxGroup = ({ field, options }: Props) => {
  return (
    <FormGroup>
      {options.map((option, i) => (
        <FormControlLabel
          key={i}
          control={
            <Checkbox
              name={field.name}
              value={option.value}
              onChange={field.onChange}
              checked={field.value.includes(option.value)}
            />
          }
          label={option.label}
        />
      ))}
    </FormGroup>
  );
};
export default CheckboxGroup

Still an issue on 2.2.9

Workaround with react-native

<CheckBox color="#000"
                      onPress={event =>
                        props.setFieldValue("terms", !props.values.terms)
                      }
                      checked={props.values.terms}
                    />

Please suggest work around for checkbox with Field render for formik 1.5.7

Yep, lost an hour or so before realising this is a straight up bug. Please re-open the issue.

The checked={value} type workaround is usable but it’s unexpected that initialValues doesn’t work

Seems that this is being fixed here #1115

I also think this is still a problem - can’t seem to set checkbox initial values using initialValues