在类组件中使用 React 更改样式组件的 CSS
我必须使用 React 项目中的 styled-components 库更改侧边栏的宽度。
这是类组件中的代码:
let SidebarStyled = styled.div`
width: 200px;
position: fixed;
left: 0px;
top: 0px;
height: 100vh;
background-color: #0c1635;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
.showFlexInMobile {
display: none !important;
}
p {
color: white;
font-size: 0.85rem;
text-align: center;
}
@media (max-width: 1024px) {
width: 70px;
.hideInMobile {
display: none !important;
}
.showFlexInMobile {
display: flex !important;
}
> .link-logo {
margin-top: 8px;
> img {
width: 50px;
}
}
}
`
const hideSidebar = () => {
// my code
}
class Sidebar extends Component<ISidebar> {
render() {
return (
<SidebarStyled>
<SidebarHeaderStyled style={{ position: 'relative' }}>
<button onClick={hideSidebar} className="buttonSideBar">
-
</button>
[...]
`;
单击末尾的按钮时,我想要一个 hideSidebar
函数来实际将宽度从 200px 更改为 70px。 我通常使用钩子来执行此操作,但我的客户端只有类组件。
任何人都可以帮助我吗? 非常感谢
I must change the width of a sidebar using styled-components library in React project.
Here is the code in the Class Component :
let SidebarStyled = styled.div`
width: 200px;
position: fixed;
left: 0px;
top: 0px;
height: 100vh;
background-color: #0c1635;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
.showFlexInMobile {
display: none !important;
}
p {
color: white;
font-size: 0.85rem;
text-align: center;
}
@media (max-width: 1024px) {
width: 70px;
.hideInMobile {
display: none !important;
}
.showFlexInMobile {
display: flex !important;
}
> .link-logo {
margin-top: 8px;
> img {
width: 50px;
}
}
}
`
const hideSidebar = () => {
// my code
}
class Sidebar extends Component<ISidebar> {
render() {
return (
<SidebarStyled>
<SidebarHeaderStyled style={{ position: 'relative' }}>
<button onClick={hideSidebar} className="buttonSideBar">
-
</button>
[...]
`;
On click on the button at the end, I would like a hideSidebar
function to actually change the width from 200px to 70px.
I usually use hooks to do this, but my client has only Class components.
Anyone can help me on this ?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 props 添加到样式标签
https://styled-components.com/docs/basics#传递的道具
在你的情况下,它看起来像
并使用它看起来
add props to your styled tags
https://styled-components.com/docs/basics#passed-props
in your case it would look something like
and use it looks
您可以尝试执行此操作
如果您想处理样式化组件中的所有样式,您可以在组件中添加一个新属性
并在组件上
它将为您工作。
You can try to do this
If you want to handle all styles in the styled component you can add a new attribute in a component
And on component
It will work for you.
嘿,代码有一些错误,所以我纠正了语法并创建了这个快速的小codepen< /a> 我不会解释我修复的所有内容,但我会说注意你的语法。您可以在 JSX 中非常简单地实现这一点,而根本不需要使用 CSS!
您应该将组件转换为功能组件,因为这是当今的惯例。但无论如何,您需要将隐藏组件的逻辑移到组件本身中。在示例中,我使用 React.useState() 切换布尔值并隐藏组件。您还可以使用 经典类组件状态管理来做到这一点。我还举了一个例子来展示它。 ;) 祝你好运
在类组件中:
Hey so the code had a few errors so I corrected the syntax and created this quick little codepen I'm not gonna explain everything I fixed but I will say watch your syntax. You can achieve this quite simply in the JSX and not have to use CSS at all!
You should convert your component over to a functional one since it's the convention these days. But regardless, you need to move the logic of hiding the component in the component itself. In the example I use React.useState() to toggle a boolean and hide the component. You can also use the classic class component state management to do this. I also put and example for showing it too. ;) Good luck
And in a class component: