代码之家  ›  专栏  ›  技术社区  ›  Taylor Austin

样式化JSX变量不工作

  •  1
  • Taylor Austin  · 技术社区  · 6 年前

    我已经使用 styled-jsx@2.2.6 我正在这样做:

    import React, { Component } from 'react'
    
    export default Index class extends Component {
      constructor(props){
       super(props)
       this.state = { clicked: false}
      }
      render(){
       return(){
        <div>
        <button onClick={() => this.setState({clicked: !this.state.clicked}}}>Hello</button>
        <style jsx>{`
        button {
         background-color: ${this.state.clicked ? 'red' : 'blue'}
        }
        `}<style>
      }
     }
    }
    

    没有应用任何样式,我控制台注销了单击状态,并且正在更改。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Rikin    6 年前

    下面是一个工作示例。你有很多打字错误。我假设您没有使用通常指出所有这些问题的适当IDE。

    以下是工作代码: https://stackblitz.com/edit/react-3ttfch

    以下是您的固定无键入代码:

    class Index extends Component {
      constructor(props) {
        super(props)
        this.state = { clicked: false }
      }
      render() {
        return (
          <div>
            <button onClick={() => this.setState({ clicked: !this.state.clicked })}>
              Hello
            </button>
            <style jsx>{`
              button {
                background-color: ${this.state.clicked ? 'red' : 'blue'}
              }
            `}</style>
          </div>
        );
      }
    }