【发布时间】:2019-12-07 05:57:42
【问题描述】:
我正在尝试将 styled-jsx 与一些代码一起使用。但是,无论我做什么,我似乎都会出错
index.js:1437 Warning: Received `true` for a non-boolean attribute `jsx`.
If you want to write it to the DOM, pass a string instead: jsx="true" or jsx={value.toString()}.
in style (at App.js:12)
in div (at App.js:9)
in Test (created by Route)
in Route (at App.js:29)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:27)
in App (at src/index.js:7)
我已经尝试重新安装 node_modules,确保我的配置一切正常,并测试了不同的版本。
我的 package.json,
{
"name": "preview",
"version": "0.1.0",
"private": true,
"dependencies": {
"contentful": "^7.4.1",
"react": "^16.8.6",
"react-dom": "^16.8.4",
"react-router-dom": "^4.3.1",
"react-scripts": "^3.0.1",
"socket.io-client": "^2.2.0",
"styled-jsx": "^3.2.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"babel": {
"presets": [
"@babel/preset-stage-2"
],
"plugins": [
"styled-jsx/babel"
]
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"proxy": "http://localhost:5000/"
}
我的示例 React 代码仍然抛出错误。
const Test = () => {
return (
<div>
<p>test
</p>
<style jsx>{
`
p {
color: red;
}
`
}
</style>
</div>)
}
class App extends Component {
render () {
return (
<Router>
<Route path='/test' component={Test}/>
</Router>
)
}
}
export default App;
我希望不会有任何基于文档的错误消息
【问题讨论】:
-
<style jsx>上的jsx应该做什么?这就是它所抱怨的。 -
这是一个 Babel 插件,可以让我使用 zeit/styled-jsx github.com/zeit/styled-jsx
-
根据报错信息,你试过
<style jsx="true">吗?但根据文档,您不需要这样做。您是如何在App.js中导入库的? -
将其设置为 true 会删除错误消息,但不会按预期工作。我不导入它,我只是将它包含在我的 package.json 作为插件中,这就是我想象它的工作方式。 React 不会加载我的 babel 插件有什么原因吗?
-
可能 Babel 插件没有运行?
标签: reactjs ecmascript-6 babeljs styled-jsx