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

13

140
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

Eirena answered 11/4, 2018 at 20:53 Comment(1)
Unrelated, but which vs code theme are you using ?Inwrought
T
110

Try this instead:

comingsoon={value ? 1 : 0}
Trifoliate answered 8/6, 2018 at 1:13 Comment(5)
This worked for me. Why does this work and not boolean?Tubular
This accepted answer is not correct, please scroll down to transient-props. Reference: styled-components.com/docs/api#transient-props.Beecham
this is what works in my case: resettable={condition ? true : undefined}Negotiate
@EvgenyBobkin is correct, the right one is using transient-props. Please see the other answerUmbilicus
typscript won't accept this :(((((((((((((((Uncourtly
T
129

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>
);
Tanya answered 1/10, 2020 at 14:34 Comment(1)
I got the following error on the console. ``` Warning: Invalid attribute name: $datasuccess ```Scarlet
T
110

Try this instead:

comingsoon={value ? 1 : 0}
Trifoliate answered 8/6, 2018 at 1:13 Comment(5)
This worked for me. Why does this work and not boolean?Tubular
This accepted answer is not correct, please scroll down to transient-props. Reference: styled-components.com/docs/api#transient-props.Beecham
this is what works in my case: resettable={condition ? true : undefined}Negotiate
@EvgenyBobkin is correct, the right one is using transient-props. Please see the other answerUmbilicus
typscript won't accept this :(((((((((((((((Uncourtly
Z
43

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

Zillah answered 14/10, 2020 at 17:4 Comment(0)
R
15

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.

Ricky answered 10/4, 2020 at 5:53 Comment(0)
C
8

Just make it a number instead, this is the workaround from https://github.com/styled-components/styled-components/issues/1198:

Cool answered 12/4, 2018 at 0:37 Comment(2)
This passes the value to the DOM and with styled-components it's not really what you want though. Even though this is an easy way to remove the warning.Roselynroseman
Using the same link, I found this solution (github.com/styled-components/styled-components/issues/…) even though some say it's not the best.Ankerite
T
8

Similar to Frank Lins answer above but I had to use undefined instead of 0 to get rid of the warning:

comingsoon={value ? 1 : undefined}
Twitch answered 15/8, 2019 at 13:44 Comment(0)
U
8

This error with styled-components appears to be due to styled() attempting to apply a boolean to an element in the DOM, but DOM elements only accept strings as attributes.

This is well documented in the styled-components repository here: https://github.com/styled-components/styled-components/issues/1198

There are two solutions:

  1. Lift the styled component w/ the passed attribute up, so that the attribute is not applied to the element directly. Or,

  2. Filter the passed attribute out of the props when calling styled components.

Both of these options are demonstrated in the code below.

CodeSandbox: https://codesandbox.io/s/cool-thunder-9w132?file=/src/App.tsx

import React, { useState } from "react";
import styled from 'styled-components';

// demonstration of two different solutions for solving the styled-components error:
// `Warning: Received `false` for a non-boolean attribute`
// Solution 1: Lift the attribute up into a wrapper.
// Solution 2: Filter-out the `toggle` attribute passed to styled-component.

interface BtnProps {
  toggle: boolean;
}

const Container = styled.div`
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
`;

const StyledBtnOne = styled.div<BtnProps>`
  & button {
    background-color: ${({toggle}) => toggle ? ' #2ecc71' : '' };
  };
`;

const StyledBtnTwo = styled(({primary, ...props}) =>
  <button {...(({toggle, ...propz}) => propz)(props)} />)<BtnProps>`
  background-color: ${({toggle}) => toggle ? ' #3498db' : '' };
`;

const App = () => {

  const [ btnOne, setBtnOne ] = useState(false);

  const [ btnTwo, setBtnTwo ] = useState(false);

  const triggerOne = () => setBtnOne(!btnOne);

  const triggerTwo = () => setBtnTwo(!btnTwo);

  return (
    <Container>
      <StyledBtnOne toggle={btnOne}>
        <button 
          onClick={triggerOne}>
            Solution 1
        </button>
      </StyledBtnOne>

      <StyledBtnTwo 
        toggle={btnTwo}
        onClick={triggerTwo}>
        Solution 2
      </StyledBtnTwo>
    </Container>
  );

}

export default App;

Unpopular answered 24/10, 2020 at 4:4 Comment(0)
A
5

Add "+" before your booleans values.

comingsoon = {+creator.comingSoon}

example below from the Link to answer

import styled from "styled-components";
import { Link } from "react-router";

const StyledLink = styled(Link)`
  color: ${({ inverted }) => (inverted ? "white" : "chartreuse")};
`;

function Navbar() {
  return (
    <nav>
      {# Bad #}
      <StyledLink inverted={true}>Home</StyledLink>

      {# Good #}
      <StyledLink inverted={+true}>About</StyledLink>
    </nav>
  );
}
Authors answered 4/4, 2022 at 1:59 Comment(1)
I can't edit this answer, but the link to the answer is broken. Here's the link it's supposed to be: maximeblanc.fr/blog/…Tellurium
D
3

This warning can be caused also if the property of styled component has the name existing in HTML. For example I had such issue when named property wrap. After renaming warning disappeared.

Delphinedelphinia answered 13/2, 2022 at 21:3 Comment(0)
R
2

Solved this by enclosing with brackets {} the boolean variable I was passing through props.

const childrenWithProps = React.Children.map(props.children, child => {
  return React.cloneElement(child, { showcard: { showCard } }
)});
Rosemari answered 8/4, 2021 at 7:8 Comment(0)
R
2

In my case, I had a class name outside of the className attribute like this:

<Col className='text-center' py-3>
    <p>Techshop &copy; {currentYear}</p>
</Col>

And then I got the error like this: received true for a non-boolean attribute

If fixed the error by moving the py-3 class into the className quotes.

<Col className='text-center py-3'>
      <p>Techshop &copy; {currentYear}</p>
</Col>
Roily answered 28/9, 2023 at 13:40 Comment(1)
I've just had something similar. I accidentally wrote "import" in my tag. It looked like this <nav import className={....}>Vaporing
K
0

I got this issue and also shows React Hydration Error in my Next.js application. In my case it seems Styled component got a custom component and it can't process 'boolean'.

Here is my workaround:

before:

styled(Text)<{ center?: boolean}>
// Text is my custom component

after:

styled.div<{ center?: boolean}>
Kosher answered 17/6, 2022 at 15:20 Comment(0)
S
0

use ...rest operator instead of passing props to the HTML.

 const Home = ({comingSoon, ...rest}) => <div {...rest}> content </div>
Semitone answered 21/3, 2023 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.