我正在尝试通过一些代码使用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;我希望不会有任何基于文档的错误消息
发布于 2021-02-20 06:10:37
好的。因为我自己在解决这个问题之前花了几个小时,所以我希望我能帮助你们也有这个问题的人。
要将styled jsx与create-react-app一起使用,您需要:
package.json中的
yarn add react-app-rewired customize-cra "start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",使用
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",package.json旁边)文件config-overrides.jsconst { override, addBabelPlugin, addBabelPreset } = require('customize-cra');
module.exports = override(
addBabelPlugin('styled-jsx/babel')
);在根目录文件.babelrc中创建
jest test)时使用
{
"plugins": ["styled-jsx/babel"]
}在您的index.js (或将React挂载到DOM的位置)中,在调用ReactDOM.render(....)之前,插入取自https://github.com/vercel/styled-jsx/issues/560#issuecomment-599371026的以下代码
const _JSXStyle = require('styled-jsx/style').default;
if (typeof global !== 'undefined') {
Object.assign(global, { _JSXStyle });
}这是不幸的,但这个复杂的配置没有它就有点反复无常。
仅当您将yarn build或yarn test与create-react-app一起使用时,才需要步骤4和5。
发布于 2022-01-25 05:49:04
此问题的答案在警告中。只需将样式的jsx属性类型从boolean转换为string即可:
来自:<style jsx> {your style here} </style>
收件人:<style jsx="true"> {your style here} </style>
发布于 2021-12-16 03:54:47
如果任何人还在受苦,请尝试在styled-jsx文档中找到以下代码片段:
import _JSXStyle from 'styled-jsx/style'
export default () => (
<div className="jsx-123">
<p className="jsx-123">only this paragraph will get the style :)</p>
<_JSXStyle id="123">{`p.jsx-123 {color: red;}`}</_JSXStyle>
</div>
)对我来说很有吸引力。
https://stackoverflow.com/questions/57261540
复制相似问题