Reactjs 样式化JSX变量不起作用
我已经使用Reactjs 样式化JSX变量不起作用,reactjs,styles,jsx,styled-components,Reactjs,Styles,Jsx,Styled Components,我已经使用styled安装了styledjsx-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>
styled安装了styledjsx-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>
}
}
}
import React,{Component}来自“React”
导出默认索引类扩展组件{
建造师(道具){
超级(道具)
this.state={clicked:false}
}
render(){
返回(){
this.setState({单击:!this.state.clicked}}>Hello
{`
钮扣{
背景色:${this.state.clicked?'red':'blue'}
}
`}
}
}
}
没有应用任何样式,我的console注销了单击状态,并且正在进行更改。这里是一个工作示例。您有很多拼写错误。我假设您没有使用通常会指出所有这些问题的适当IDE 以下是工作代码: 以下是您的固定无键入代码:
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>
);
}
}
类索引扩展组件{
建造师(道具){
超级(道具)
this.state={clicked:false}
}
render(){
返回(
this.setState({单击:!this.state.clicked})}>
你好
{`
钮扣{
背景色:${this.state.clicked?'red':'blue'}
}
`}
);
}
}
您是否将样式化jsx添加到您的babel插件中?我正在使用gatsby。它有一个gatsby插件样式化jsx
,位于gatsby配置中。
。但我还没有在.babelrc中这样做,我该如何做?{“插件”:[“styled jsx/babel”]}现在我得到了错误的Uncaught SyntaxError:Unexpected identifier
在这个过程的哪个部分出现了错误,你能发布整个输出吗?我需要向样式化jsx添加一个插件,这样我就可以使用嵌套了。我试着在babelrc中这样做:[“styled jsx/babel”,{“plugins”:[“styled jsx plugin postcss”]]
但我只是在控制台中获得了未捕获的SyntaxError:意外标识符
。这些都是在构建过程中使用的插件,假设您的代码符合JS sytax。在您的情况下,情况并非如此,正如我所说,您有太多的拼写错误。您可以单击上面提供的链接,查看它是否正常工作。这是一样的没有语法错误的代码。