[英]Warning: Received `false` for a non-boolean attribute. How do I pass a boolean value?
The below code is giving me this error:下面的代码给了我这个错误:
react-dom.development.js:67 Warning: Received
false
for a non-boolean attributehigh
.react-dom.development.js:67 警告:收到
false
的非布尔属性high
。 If you want to write it to the DOM, pass a string instead: high="false" or high={value.toString()}.如果要将其写入 DOM,请改为传递字符串:high="false" 或 high={value.toString()}。
I have tried various stuff but the color of the button doesn't change based on the prop value.我尝试了各种东西,但按钮的颜色不会根据道具值而改变。
import styled from 'styled-components';
const Button=styled.button`
border:none;
background:${(props) => (props.high ? 'red':'orange')};
color:white;
`
export const getStaticProps = async () => {
const res = await fetch('http://localhost:8000/issues');
const data = await res.json();
return {
props: { cards: data }
}
}
const issues = ({cards}) => {
return (
<>
<div className="container">
{cards.map(card => (
<div className="grid-item link" key={card.id}>
<Link href={'/issues/' + card.id}>
<div>
<Button high={card.priority === "high"} className="priority">{ card.priority } Priority</Button>
</div>
</Link>
</div>
))}
</div>
</>
);
}
There are two separate issues here - the error you are receiving and the fact that the button doesn't respond to the priority prop changing.这里有两个单独的问题 - 您收到的错误以及按钮不响应优先级道具更改的事实。
Let's address the first problem - when React tells you that it received a boolean value for a non boolean attribute, it means that the attribute 'high' you've used for the button element isn't recognized as a legal attribute for the native html element - and therefore React is warning you that passing a value to that attribute doesn't make any sense.让我们解决第一个问题 - 当 React 告诉您它收到一个非 boolean 属性的 boolean 值时,这意味着您用于按钮元素的属性“高”未被识别为本机 ZFC35FDC70D5FC69A2A326 的合法属性元素 - 因此 React 警告您将值传递给该属性没有任何意义。
But where are you doing that?但是你在哪里做呢? Ostensibly you are using a non native Button component, returned by styled-components, so where are you passing the attribute to the actual native element?
从表面上看,您使用的是由样式组件返回的非本地 Button 组件,那么您在哪里将属性传递给实际的本地元素? Well, what happens is, styled-components will basically pass through any props that you pass to it - so if you've wrapped the native button component with the styled function, the attribute "high" will be passed to the native underlying button.
好吧,发生的事情是,样式组件基本上会通过您传递给它的任何道具 - 因此,如果您使用样式 function 包装本机按钮组件,则属性“high”将传递给本机底层按钮。
Is that really a problem?这真的是个问题吗? Honestly I don't think it's that terrible, and you can maybe just ignore the warning if you want - it will look strange though that the actual generated html markup will include this attribute - you can confirm this through a browser element inspector.
老实说,我不认为这很糟糕,如果你愿意,你可以忽略警告 - 虽然实际生成的 html 标记将包含此属性,但它看起来会很奇怪 - 你可以通过浏览器元素检查器确认这一点。
If you do want to solve this warning, I know of two ways:如果您确实想解决此警告,我知道两种方法:
const Button = styled.button`
border: none;
background: orange;
&.high {
background: red;
}
color: white;
`
<Button className={clsx("priority", {
high: priority==="high"
})}
/>
You'll notice I've used a utility named clsx for conditionally concatenating classes, there are alternatives like classnames .您会注意到我使用了一个名为clsx的实用程序来有条件地连接类,还有一些替代方法,例如classnames 。 You can also accomplish this with vanilla concatenating the classes.
你也可以通过 vanilla 连接类来实现这一点。 This solution has the downside of potential classname clashes - so it must be used carefully.
这个解决方案有潜在的类名冲突的缺点——所以必须小心使用。 You may mitigate this concern with a more specific class like "high-priority-button".
您可以使用更具体的 class (例如“高优先级按钮”)来缓解这种担忧。
const Button = styled(
({high, ...rest} )=> <button {...rest}/>
)`
border: none;
background: ${(props) => (props.high ? 'red':'orange')};
color: white;
`
This way you dont pass the "high" attribute to the native button, and it is only used for the styled components functional interpolation.这样您就不会将“high”属性传递给本机按钮,它仅用于样式化组件功能插值。 The downside to this is that it is cumbersome and verbose, and potentially slightly less performant because it adds another function-call layer.
这样做的缺点是它既麻烦又冗长,而且由于它添加了另一个函数调用层,因此性能可能会稍差一些。
Your other problem, where the color doesn't actually change, seems unrelated and may be caused by other reasons, so this warning is probably a misdirection.你的另一个问题,颜色实际上没有改变,似乎无关,可能是由其他原因引起的,所以这个警告可能是一个误导。 I suggest logging the priority prop somewhere, and you may be able to see if the priority actually does change when it is supposed to, before it reaches the component.
我建议在某处记录优先级道具,您可以在优先级到达组件之前查看优先级是否确实在它应该改变的时候发生了变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.