Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Received `false` for a non-boolean attribute. How do I pass a boolean for a custom boolean attribute?

Warning: Received `false` for a non-boolean attribute `comingsoon`.

If you want to write it to the DOM, pass a string instead: 
comingsoon="false" or comingsoon={value.toString()}.

How do I pass a boolean in a custom attribute for React?

I'm using styled-components and passing the attribute through the component. Here is a picture of how I'm passing the attr.

passing boolean custom attr as "comingsoon"

styled-components css props

like image 559
desilijic Avatar asked Apr 11 '18 20:04

desilijic


4 Answers

Try this instead:

comingsoon={value ? 1 : 0}
like image 67
Frank Lin Avatar answered Oct 20 '22 16:10

Frank Lin


As of 5.1 you can now use transient props ($) which prevents the props being passed to the DOM element.

const Comp = styled.div`
  color: ${props =>
    props.$draggable || 'black'};
`;

render(
  <Comp $draggable="red" draggable="true">
    Drag me!
  </Comp>
);
like image 29
Hansel Avatar answered Oct 20 '22 15:10

Hansel


You have to add $ prefix to attribute:

$comingsoon={value}

Styled Components had added transient props in 5.1 version: https://styled-components.com/docs/api#transient-props

like image 30
Anton Starcev Avatar answered Oct 20 '22 17:10

Anton Starcev


In my case, it was because I was accidentally passing {...@props} down into a div.

Usually passing attribute={false} is fine, but not to native elements.

like image 13
dansch Avatar answered Oct 20 '22 15:10

dansch