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.

Why is the value of a checkbox using `useCheckbox` `''` instead of `'on'`?

See original GitHub issue

Discussed in https://github.com/adobe/react-spectrum/discussions/3516

<div type='discussions-op-text'>

Originally posted by brandonpittman September 14, 2022 Ran into this today and was wondering why is the value of a checkbox using useCheckbox '' instead of 'on'?

I’m not passing it a value, but it’s getting assigned to an empty string.

</div>

A checkbox’s default behavior when it doesn’t have an explicit value is to send 'on' when submitted in a form.

From MDN:

If the value attribute was omitted, the default value for the checkbox is on, so the submitted data in that case would be subscribe=on

Also, this example you shared creates a checkbox with a value attribute that has no value.

The useCheckbox examples in the docs also present with this problem.

So the server pulls '' out of the FormData. The expected behavior is <input type="checkbox" > shouldn’t have a value attribute in the DOM.

In the following example, you’ll see that useCheckbox applies an empty value attribute. The default checkbox doesn’t have that. When this usually occurs is when you apply value={null || undefined}which I now believe useToggle is doing here.

We don’t come up with a value for you because that usually needs to be a string that matches something on your server should you want to submit a form. So it’s empty until you supply one. The value doesn’t matter if you are submitting the data via js because you’d store the data in some js object.

If you expect the browser’s default behavior and submit a form normally, this is a problem.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sonithchcommented, Sep 25, 2022

@snowystinger Hi, thanks for the references.

Passing null in the value attribute logs a warning in the console by default. So, we omit only the undefined value in useToggle in the same way as MUI even if it’s passed intentionally?

Something like this ...(value !== undefined && {value})

Let me know if this is the right way, so that I can raise a PR for the changes in useToggle.

1reaction
sonithchcommented, Sep 25, 2022

@snowystinger I did confirm the issue is caused by the value=undefined returned by the useToggle

The idea is to return the value attribute from the useToggle if it’s only passed via props to it, else do not return it.

So, instead of value, return the expression ...(Object.prototype.hasOwnProperty.call(props, 'value') && {value}) in useToggle which checks the if the value is passed in props and then returns it. This makes sure the checkbox value is on if the value is not passed.

Let me know if this is the right way to go.

Read more comments on GitHub >

github_iconTop Results From Across the Web

<input type="checkbox"> - HTML: HyperText Markup Language
A checkbox allows you to select single values for submission in a form ... a hyphen or minus sign) instead of a check/tick...
Read more >
Use Checkbox to set value instead of DropDown
I have a DrowDown1 connected to a Datasource (SharePoint List). The user should be able to check a CheckBox1 and when sending the...
Read more >
HTML Input Checkbox return 'On' instead of 'True' when ...
When I submit the form containing this element, its post value is 'on' instead of the expected 'true' (when element is checked). Does...
Read more >
useCheckbox – React Aria
Checkbox supports the name and value props for integration with HTML forms.
Read more >
PowerApps - Use checkbox instead of toggle - YouTube
PowerApps - Use checkbox instead of toggleIf you would like to explore more or sign up to Microsoft 365 here are some useful...
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