在样式组件 React 中忽略父样式中的 CSS
我正在使用使用样式组件的现有组件库创建一个 React 组件。我有时需要覆盖父库中的样式并使用我自己的样式。这是库中具有样式的 TextInput 组件,
border: 2px solid black;
在使用此组件时,我不希望输入框周围有边框。相反,我只想要一条下划线。
我用两种方法做到了 使用样式道具,
<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{borderTop: 'none',borderRight: 'none',
borderLeft:'none', borderBottom: '2px solid black'}}
/>
通过这个我得到一个黑色边框底部,但它需要我对所有其他三个重复。有没有更好的方法让我可以写一行来解决我的问题?
我根据现有组件创建了一个样式组件,
const TextInputStyled = styled(TextInput)`
border-top: none;
border-right: none;
border-left: none;
border-bottom: 1px solid black`;
这是我具有上述样式的组件。
<TextInputStyled
name="test-input"
value={testInput}
onChange={onInputChange}
/>
在这种情况下,我得到的边框底部为 2px 黑色,但另一个边框也存在,这也是重复。
有更好的方法吗?我经常需要调整父组件的样式。
非常感谢任何帮助。
I am creating a React component using an existing component library that uses styled component. I time to time need to override the style from the parent library and use my own. Here is a TextInput component from the library that has style,
border: 2px solid black;
while using this component I don't want to have the border around the input box. Instead, I just want one underline.
I did it two ways,
Using style props,
<TextInput
name="test-input"
value={testInput}
onChange={onInputChange}
style={{borderTop: 'none',borderRight: 'none',
borderLeft:'none', borderBottom: '2px solid black'}}
/>
By this I get a black border-bottom but it requires me to repeat for all other three. Is there a better way so that I can write one line and that solves my issue ?
I created a styled component based on the existing component,
const TextInputStyled = styled(TextInput)`
border-top: none;
border-right: none;
border-left: none;
border-bottom: 1px solid black`;
And here my component with the above style.
<TextInputStyled
name="test-input"
value={testInput}
onChange={onInputChange}
/>
In this case I get the border-bottom with 2px black but the other border also exist and this is also a repetition.
Is there a better to do this ? I often need to twitch the style of the parent component.
Any help is very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你为什么不尝试这个代码:
更新:
如果你坚持使用样式组件,ThemeProvider是解决方案:
如果你不了解ThemeProvider,请阅读这篇文章:
https://styled-components.com/docs/advanced
why don't you try this code:
Update:
If you insist on using styled component, ThemeProvider is the solution:
If you don't know about ThemeProvider read this article:
https://styled-components.com/docs/advanced