在类组件中使用 React 更改样式组件的 CSS

发布于 2025-01-09 08:04:22 字数 1279 浏览 0 评论 0原文

我必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救星 2025-01-16 08:04:22

将 props 添加到样式标签

https://styled-components.com/docs/basics#传递的道具
在你的情况下,它看起来像

const Somestyled= styled.input`
  padding: 0.5em;
  margin: 0.5em;
  width: ${props => props.somethinghere};
  background: papayawhip;
  border: none;
  border-radius: 3px;
`;

并使用它看起来

<Somestyled somethinghere={yourVariableHere} />

add props to your styled tags

https://styled-components.com/docs/basics#passed-props
in your case it would look something like

const Somestyled= styled.input`
  padding: 0.5em;
  margin: 0.5em;
  width: ${props => props.somethinghere};
  background: papayawhip;
  border: none;
  border-radius: 3px;
`;

and use it looks

<Somestyled somethinghere={yourVariableHere} />
泪意 2025-01-16 08:04:22

您可以尝试执行此操作

//your styled component

class Sidebar extends Component<ISidebar> {
  constructor( props ){
    super( props );
    this.state = { sidebarShow: true };
    this.hideSidebar = this.hideSidebar .bind(this);
  }

  hideSidebar = () => {
    // your code
    this.setState({
      ...this.state,
      sidebarShow: false
    })
  }
  render() {
    return (
      <SidebarStyled style={{left: `${this.state.sidebarShow ? '0px' : '-250px'}` }}>
        <SidebarHeaderStyled style={{ position: 'relative'}}>
          <button onClick={this.hideSidebar} className="buttonSideBar">
            -
          </button>
   [...]
`;

如果您想处理样式化组件中的所有样式,您可以在组件中添加一个新属性

<SidebarStyled isShow={this.state.sidebarShow} >
....
</SidebarStyled>

并在组件上

let SidebarStyled = styled.div`
  width: 200px;
  position: fixed;
  left: ${props=>props.isShow ? '0px': '-250px'};

...

`

它将为您工作。

You can try to do this

//your styled component

class Sidebar extends Component<ISidebar> {
  constructor( props ){
    super( props );
    this.state = { sidebarShow: true };
    this.hideSidebar = this.hideSidebar .bind(this);
  }

  hideSidebar = () => {
    // your code
    this.setState({
      ...this.state,
      sidebarShow: false
    })
  }
  render() {
    return (
      <SidebarStyled style={{left: `${this.state.sidebarShow ? '0px' : '-250px'}` }}>
        <SidebarHeaderStyled style={{ position: 'relative'}}>
          <button onClick={this.hideSidebar} className="buttonSideBar">
            -
          </button>
   [...]
`;

If you want to handle all styles in the styled component you can add a new attribute in a component

<SidebarStyled isShow={this.state.sidebarShow} >
....
</SidebarStyled>

And on component

let SidebarStyled = styled.div`
  width: 200px;
  position: fixed;
  left: ${props=>props.isShow ? '0px': '-250px'};

...

`

It will work for you.

甜妞爱困 2025-01-16 08:04:22

嘿,代码有一些错误,所以我纠正了语法并创建了这个快速的小codepen< /a> 我不会解释我修复的所有内容,但我会说注意你的语法。您可以在 JSX 中非常简单地实现这一点,而根本不需要使用 CSS!

您应该将组件转换为功能组件,因为这是当今的惯例。但无论如何,您需要将隐藏组件的逻辑移到组件本身中。在示例中,我使用 React.useState() 切换布尔值并隐藏组件。您还可以使用 经典类组件状态管理来做到这一点。我还举了一个例子来展示它。 ;) 祝你好运

const Sidebar = () => {
  const [isShown, setIsShown] = React.useState(true);
  
  const hideSidebar = () => {
    setIsShown(false)
  }

  const showSidebar = () => {
    setIsShown(true)
  }
  return (
    <div>
    {
      isShown &&
      (<SidebarStyled>
      <button onClick={hideSidebar} className="buttonSideBar">
        -
      </button>
    </SidebarStyled>)}
      {!isShown && <button onClick={showSidebar} style={{ width: 50, height: 50, marginLeft: 300}}>
        show
      </button>}
    </div>
  );          
}

在类组件中:

class Sidebar extends React.Component {
 constructor(props) {
   super(props)
   
   this.state = { isShown: false };
 }
  
 hideSidebar() {
    this.setState({ isShown: false});
 }

  showSidebar() {
    this.setState({ isShown: true});
  }
  
  render() {
    const {
      isShown
    } = this.state;
    
    return (
    <div>
    {
      isShown &&
      (<SidebarStyled>
      <button onClick={() => this.hideSidebar()} className="buttonSideBar">
        -
      </button>
    </SidebarStyled>)}
      {!isShown && <button onClick={() => this.showSidebar()} style={{ width: 50, height: 50, marginLeft: 300}}>
        show
      </button>}
    </div>
  ); 
  }
           
}

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

const Sidebar = () => {
  const [isShown, setIsShown] = React.useState(true);
  
  const hideSidebar = () => {
    setIsShown(false)
  }

  const showSidebar = () => {
    setIsShown(true)
  }
  return (
    <div>
    {
      isShown &&
      (<SidebarStyled>
      <button onClick={hideSidebar} className="buttonSideBar">
        -
      </button>
    </SidebarStyled>)}
      {!isShown && <button onClick={showSidebar} style={{ width: 50, height: 50, marginLeft: 300}}>
        show
      </button>}
    </div>
  );          
}

And in a class component:

class Sidebar extends React.Component {
 constructor(props) {
   super(props)
   
   this.state = { isShown: false };
 }
  
 hideSidebar() {
    this.setState({ isShown: false});
 }

  showSidebar() {
    this.setState({ isShown: true});
  }
  
  render() {
    const {
      isShown
    } = this.state;
    
    return (
    <div>
    {
      isShown &&
      (<SidebarStyled>
      <button onClick={() => this.hideSidebar()} className="buttonSideBar">
        -
      </button>
    </SidebarStyled>)}
      {!isShown && <button onClick={() => this.showSidebar()} style={{ width: 50, height: 50, marginLeft: 300}}>
        show
      </button>}
    </div>
  ); 
  }
           
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文