如何使用样式的组件和ReactJ添加自定义CSS?警告:因非树立属性而被收到false
我喜欢根据道具添加样式。
import {ArrowBackIcon} from './styles';
const App = () => {
...
const isFirstImageAttachment = (index) => {
return (
filteredImages.length &&
filteredImages[0]?.uuid === attachments[index]?.uuid
);
};
...
return (
<div className="App">
...
<ArrowBackIcon
isfirstimageattachment={isFirstImageAttachment(idx)}
onClick={clickBackAttachment}
/>
...
</div>
);
};
)
styles.js
export const ArrowForwardIcon = styled(ForwardArrowIcon)`
...
display: ${props => (props.isfirstimageattachment ? 'none' : 'block')}; ;
`;
isfirstimageattachment
应该返回true
或false
,因此取决于我喜欢隐藏或显示arrowforwardicon
>组件。
我会遇到一个错误,说警告:非先进属性的false isfirstimageattachment。如果您想将其写入DOM,请改用一个字符串:IsfirstimageatTachment =“ false”或isfirstimagageattachment = {value.toString(value.toString(value.tostring(value.tostring)) )}
我不想做isfirstimageattachment =“ false”
,因为isfirstimageattachment
并不总是返回false。另外,此函数不会返回字符串
so isfirstimageatTachment = {value.tostring()}}
不按我想要的方式工作。
我能做什么?
我发现的尝试
是,即使我传递了字符串false
样式没有改变。
return (
<div className="App">
...
<ArrowBackIcon
isfirstimageattachment="false"
onClick={clickBackAttachment}
/>
...
</div>
我看到元素被应用了显示:无;
这不是将道具提供给样式组件的方式吗?
I like to add styles based on the props.
import {ArrowBackIcon} from './styles';
const App = () => {
...
const isFirstImageAttachment = (index) => {
return (
filteredImages.length &&
filteredImages[0]?.uuid === attachments[index]?.uuid
);
};
...
return (
<div className="App">
...
<ArrowBackIcon
isfirstimageattachment={isFirstImageAttachment(idx)}
onClick={clickBackAttachment}
/>
...
</div>
);
};
)
styles.js
export const ArrowForwardIcon = styled(ForwardArrowIcon)`
...
display: ${props => (props.isfirstimageattachment ? 'none' : 'block')}; ;
`;
isFirstImageAttachment
is supposed to return true
or false
so depending on that value I like to hide or show ArrowForwardIcon
component.
I'm getting an error saying Warning: Received false for a non-boolean attribute isfirstimageattachment.If you want to write it to the DOM, pass a string instead: isfirstimageattachment="false" or isfirstimageattachment={value.toString()}
I don't want to do isfirstimageattachment="false"
because isfirstimageattachment
doesn't always return false. Also this function doesn't return string
so isfirstimageattachment={value.toString()}
don't work the way I want to.
Is there any I can do?
Attempts
What I figured out is even though I passed string false
style didn't change.
return (
<div className="App">
...
<ArrowBackIcon
isfirstimageattachment="false"
onClick={clickBackAttachment}
/>
...
</div>
I saw the element got applied display: none;
Is this not the way giving the props to styled component?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
styles.js
我能够以这种方式动态添加样式!
styles.js
I was able to add style dynamically this way!