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.

[labs/react] boolean attributes should allow false to be more react-like

See original GitHub issue

Description

A boolean attribute, by definition, is true when it exists on the element, regardless of the value. There is no hidden="false", the false value would be removing the attribute. React allows false values for things like checked, so it’s confusing to have to do something like hidden={showElement ? undefined : true} on a boolean attribute.

We have this issue on our Modals.

Steps to Reproduce

set hidden={false} on a CdsModal and notice that it does not show, as expected.

Live Reproduction Link

https://stackblitz.com/edit/clarity-react-8em3m2?file=src%2FApp.tsx

Expected Results

The hidden prop can accept a value of false and behave like undefined.

Actual Results

false is the same as true

Browsers Affected

  • Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ashleyryancommented, Nov 10, 2022

Figured it out! It was an async timing issue where I was checking for the hidden attribute before the first render was finished. It passes, looks like our workaround can safely be removed. Thanks for fixing this!

I should note that I only had this issue with the hidden attribute on a few components - I didn’t use anything like the id example you gave above. I also think we had a component with an attribute that took a “true” or “false” or undefined value as 3 separate things, so we should check that one as well. (But again I’m not on that team or at that company anymore, so I’ll give them a nudge)

1reaction
ashleyryancommented, Nov 9, 2022

Thanks for this @taylor-vann - I no longer work on the project that was having the issue, but I’ll pass it on to the team. I did clone the repo and checked it out really quick and it most of the jest tests have passed as expected when I remove the higher order component I build to convert false to undefined. One is failing still and I haven’t had a chance to dig into it yet, but it looks good for the most part!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactive properties
Lit manages your reactive properties and their corresponding attributes. ... Boolean properties that expose an attribute should default to false. For more ......
Read more >
Lit for React Developers
Lit is a simple library for building fast, lightweight web components that work in any framework, or with no framework at all. With...
Read more >
How to Avoid the “Boolean Trap” When Designing React ...
But using CSS conventions à la Bootstrap for the API design of your React components can lead to bad decisions. The most famous...
Read more >
Uncontrolled Components
To handle this case, you can specify a defaultValue attribute instead of value . ... In HTML, an <input type="file"> lets the user...
Read more >
State Management with Apollo Client (Reactive Variables)
Introduction State management is one of the most common problems for ... because React is a library, not a framework), developers have to...
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