Tailwind-Styled-Component: Received `true` for a non-boolean attribute `block`
It seems like props are passed as an attribute and when I pass boolean, It throws error on the console. I am using next.js with typescript.
This is the error
Warning: Received `true` for a non-boolean attribute `block`.
If you want to write it to the DOM, pass a string instead: block="true" or block={value.toString()}.
button
functionTemplate/</<@webpack-internal:///./node_modules/tailwind-styled-components/dist/tailwind.js:34:101
Component looks like this
import tw from 'tailwind-styled-components';
interface Props {
layout?: 'primary' | 'outline';
disabled?: boolean;
block?: boolean;
size?: 'small' | 'medium' | 'large';
}
const Button = tw.button`
flex justify-center items-center border border-solid font-medium rounded-sm transition-all duration-300 ease-in-out
${(props: Props) =>
props.layout === 'primary' &&
'border-primary-400 bg-primary-400 text-white hover:bg-primary-500 hover:border-primary-600'}
${(props: Props) =>
props.layout === 'outline' &&
'border-gray-300 text-gray-700 hover:border-blue-500 hover:text-blue-500'}
${(props: Props) =>
props.disabled && 'bg-gray-50 border-gray-200 text-gray-200'}
${(props: Props) => props.size === 'small' && 'px-2 py-2 text-xs'}
${(props: Props) => props.size === 'medium' && 'px-4 py-3 text-xs'}
${(props: Props) => props.size === 'large' && 'px-5 py-4 text-lg'}
${(props: Props) => props.block && 'w-full'}
`;
export default Button;
The workaround is using number as boolean block ? 1 : 0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (16 by maintainers)
New NPM package released with transient props support and way better typing. Many thanks to @m50 and @cemreinanc for your help!
Yeah I read the same stack overflow.
Perfect then we will implement it once typescript 4.4 is out.
I’ll just document it for now.
Thank you so much for your help 🙏
Hello,
Yes I have the same issue, apparently Styled component has it as well. The only clean solution would be to sanitize the property based on the tag. So your button would get the prop “block” removed as it’s not a default HTML prop. But that would require to ship the list of all the HTML props of every tags in the package and I need to make sure that this will not end up in production or it will add a lot of useless code to the prod bundle.
If you find someone who implemented a clean solution let me know 😉 Otherwise using number instead of boolean is a good quick fix.