des*_*jic 16 reactjs styled-components react-16
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()}.
Run Code Online (Sandbox Code Playgroud)
如何在React的自定义属性中传递布尔值?
我正在使用样式组件并通过组件传递属性.这是我如何通过attr的图片.
Mus*_*ill 30
截至5.1您现在可以使用瞬态道具,以防止道具被传递给 DOM 元素。
const Comp = styled.div`
color: ${props =>
props.$draggable || 'black'};
`;
render(
<Comp $draggable="red" draggable="true">
Drag me!
</Comp>
);
Run Code Online (Sandbox Code Playgroud)
小智 22
试试这个:
comingsoon={value ? 1 : 0}
Run Code Online (Sandbox Code Playgroud)
小智 21
您必须$为属性添加前缀:
$comingsoon={value}
Run Code Online (Sandbox Code Playgroud)
Styled Components 在 5.1 版本中添加了瞬态道具:https : //styled-components.com/docs/api#transient-props
此错误styled-components似乎是由于styled()尝试将布尔值应用于 DOM 中的元素,但 DOM 元素仅接受字符串作为属性。
这在此处的存储库中有详细记录styled-components:https://github.com/styled-components/styled-components/issues/1198
有两种解决方案:
将带有传递的属性的样式组件向上提升,以便该属性不会直接应用于元素。或者,
调用样式组件时,将传递的属性从 props 中过滤掉。
下面的代码演示了这两个选项。
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;
Run Code Online (Sandbox Code Playgroud)
类似于上面的 Frank Lins 回答,但我不得不使用 undefined 而不是 0 来消除警告:
comingsoon={value ? 1 : undefined}
Run Code Online (Sandbox Code Playgroud)
只需将其改为一个数字,这是来自https://github.com/styled-components/styled-components/issues/1198的解决方法:
小智 5
在布尔值之前添加“+”。
comingsoon = {+creator.comingSoon}
Run Code Online (Sandbox Code Playgroud)
下面的示例来自回答链接
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>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19187 次 |
| 最近记录: |