question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CheckboxWithLabel returns empty array instead of true

See original GitHub issue

Below is how I am implementing the checkbox.

                          <Field
                                Label={{label: 'Remember me'}}
                                checked={false}
                                onChange={() => console.log('checked')}
                                name="remember"
                                value="1"
                                color="primary"
                                component={CheckboxWithLabel}
                            />

When I check the box it returns empty array instead of boolean. This causes the error below:

Warning: Failed prop type: Invalid prop `checked` of type `array` supplied to `ForwardRef(Checkbox)`, expected `boolean`.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:22

github_iconTop GitHub Comments

4reactions
rideroddcommented, Nov 15, 2019

I’ve managed to get i work but couldn’t use formik-material-ui, here’s a hack while waiting for a fix:

import { Formik, Form, Field, ErrorMessage } from 'formik';
import Switch from '@material-ui/core/Switch';
import FormControlLabel from '@material-ui/core/FormControlLabel';

<Formik initialValues={{ switch: false }}>{form => (
    <Form className="form">
        <FormControlLabel
          control={<Field name="switch" component={Switch} />}
          label="My label"
          name="switch"
          required={true}
          onChange={(ev, checked) => { form.setFieldValue('terms', checked); }}
        />
        <ErrorMessage name="terms" />
    </Form>
)}</Formik>

Using onChange prop I can manually set the form state using the Formik context.

3reactions
r3micommented, Nov 24, 2019

For Switch (boolean behaviour i.e. not multiple), I managed to make it work in Formik 2 natively (without formik-material-ui), using <Field as> syntax. Extract:

import Switch from "@material-ui/core/Switch"
import FormControlLabel from "@material-ui/core/FormControlLabel"
import { Field, Form, Formik } from "formik"
...<Formik><Form>...
              <FormControlLabel
                control={
                  <Field
                    name="someBool"
                    id="someBool"
                    type="checkbox" // seems required to handle correctly initial values of true
                    as={Switch}
                  />
                }
                label="Some Boolean"
              />

using formik 2.0.6 and material-ui 4.6.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

CheckboxWithLabel returns empty array instead of true #114
Below is how I am implementing the checkbox. ... When I check the box it returns empty array instead of boolean. This causes...
Read more >
Checkbox input returning empty string instead of true/false ...
I think the error here is because you are not actually passing a checked property to the <Checkbox> component.
Read more >
Building a custom checkbox in React - LogRocket Blog
Default and custom checkboxes in React. The checkbox is a control element that provides an option to toggle between true and false states....
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
inputInstance.indeterminate = true; ... in the box (it looks somewhat like a hyphen or minus sign) instead of a check/tick in most browsers....
Read more >
Checkbox class - material library - Flutter - Dart API docs
A Material Design checkbox. The checkbox itself does not maintain any state. Instead, when the state of the checkbox changes, the widget calls...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found