相关疑难解决方法(0)

样式化组件中的条件呈现

如何在样式组件中使用条件呈现,使用React中的样式组件将我的按钮类设置为活动状态?

在CSS中我会这样做:

<button className={this.state.active && 'active'} onClick={ () => this.setState({active: !this.state.active}) }>Click me</button>

在样式组件中如果我尝试在类名中使用'&&'它不喜欢它.

import React from 'react'
import styled from 'styled-components'

const Tab = styled.button`
  width: 100%;
  outline: 0;
  border: 0;
  height: 100%;
  justify-content: center;
  align-items: center;
  line-height: 0.2;
`

export default class Hello extends React.Component {
  constructor() {
    super()
    this.state = {
      active: false
    }  
    this.handleButton = this.handleButton.bind(this)
}

  handleButton() {
    this.setState({ active: true })
  }

  render() {
     return(
       <div>
         <Tab onClick={this.handleButton}></Tab>
       </div>
     )
  }}
Run Code Online (Sandbox Code Playgroud)

reactjs styled-components

35
推荐指数
5
解决办法
3万
查看次数

标签 统计

reactjs ×1

styled-components ×1