我收到一条错误信息
Warning: Received `false` for a non-boolean attribute `active`.
If you want to write it to the DOM, pass a string instead: active="false" or active={value.toString()}.
If you used to conditionally omit it with active={condition && value}, pass active={condition ? value : undefined} instead.
我的代码看起来像这样
index.jsx
import { Container, CalendarIcon } from "./styles";
export default function App() {
const isActive = context => context === activePopover;
return (
<Container>
...
<CalendarIcon active={isActive(POPOVERS.Date)} />
...
</Container>
);
}
styles.js
import styled from 'styled-components';
import {default as CalendarRoundIconSvg} from './CalendarRoundIcon.tsx';
export const isActive = active => (active ? `#04435e` : `#9b9fa3`);
export const CalendarIcon = styled(CalendarRoundIconSvg).attrs(({active}) => ({
color: isActive(active),
}))`
`;
CalendarRoundIcon.tsx
const CalendarRoundIcon: React.FC<SvgIconProps> = props => {
const {color = 'currentColor'} = props;
return (
<SvgIcon
width="14"
height="14"
viewBox="0 0 14 14"
fill={color}
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<path
d="M4.47109 8.49349H3.51286C3.40199 8.49313 3.29577 8.45107 3.21737 8.37648C3.13897 8.30189 3.09476 8.20082 3.09439 8.09534V7.18363C3.09476 7.07808 3.13895 6.97695 3.21733 6.90225C3.2957 6.82755 3.40192 6.78534 3.51286 6.7848H4.47109C4.58209 6.78534 4.68839 6.82753 4.76688 6.90221C4.84537 6.97689 4.88971 7.07802 4.89027 7.18363V8.09534C4.88971 8.20089 4.84534 8.30195 4.76683 8.37652C4.68832 8.45109 4.58202 8.49314 4.47109 8.49349Z"
fill={color}
/>
</SvgIcon>
);
};
export default memo(CalendarRoundIcon);
index.jsx
有一个isActive
函数,返回布尔值,我将这个函数作为active props
传递给样式化的组件。
在样式组件(styles.js
)中,还有一个函数基于从index.jsx
传递的active props
返回颜色十六进制。
最后将颜色传递给图标文件。
我怎样才能摆脱这个警告?
发布于 2022-08-03 15:06:28
在$active
上添加CalendarIcon解决了这个问题!
import { Container, CalendarIcon } from "./styles";
export default function App() {
const isActive = context => context === activePopover;
return (
<Container>
...
<CalendarIcon $active={isActive(POPOVERS.Date)} />
...
</Container>
);
}
另外,styled.jsx,您需要将单词active
改为$active
import styled from 'styled-components';
import {default as CalendarRoundIconSvg} from './CalendarRoundIcon.tsx';
export const isActive = active => (active ? `#04435e` : `#9b9fa3`);
export const CalendarIcon = styled(CalendarRoundIconSvg).attrs(({$active}) => ({
color: isActive($active),
}))`
`;
https://stackoverflow.com/questions/73212223
复制相似问题