首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告:收到非布尔属性`jsx`的`true`。Zeit样式的Jsx

警告:收到非布尔属性`jsx`的`true`。Zeit样式的Jsx
EN

Stack Overflow用户
提问于 2019-07-30 05:18:09
回答 5查看 3.9K关注 0票数 6

我正在尝试通过一些代码使用styled-jsx。然而,无论我做什么,我似乎都会得到一个错误

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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代码仍然会抛出错误。

代码语言:javascript
复制
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;

我希望不会有任何基于文档的错误消息

EN

回答 5

Stack Overflow用户

发布于 2021-02-20 06:10:37

好的。因为我自己在解决这个问题之前花了几个小时,所以我希望我能帮助你们也有这个问题的人。

要将styled jsx与create-react-app一起使用,您需要:

package.json中的

  1. yarn add react-app-rewired customize-cra
  2. replace

代码语言:javascript
复制
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",

使用

代码语言:javascript
复制
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",

  1. 创建(在根目录中,package.json旁边)文件config-overrides.js

代码语言:javascript
复制
const { override, addBabelPlugin, addBabelPreset } = require('customize-cra');
module.exports = override(
    addBabelPlugin('styled-jsx/babel')
);

在根目录文件.babelrc中创建

  1. (仅在运行jest test)

时使用

代码语言:javascript
复制
{
    "plugins": ["styled-jsx/babel"]
}

在您的index.js (或将React挂载到DOM的位置)中,在调用ReactDOM.render(....)之前,插入取自https://github.com/vercel/styled-jsx/issues/560#issuecomment-599371026的以下代码

代码语言:javascript
复制
const _JSXStyle = require('styled-jsx/style').default;
if (typeof global !== 'undefined') {
    Object.assign(global, { _JSXStyle });
}

这是不幸的,但这个复杂的配置没有它就有点反复无常。

仅当您将yarn buildyarn test与create-react-app一起使用时,才需要步骤4和5。

票数 5
EN

Stack Overflow用户

发布于 2022-01-25 05:49:04

此问题的答案在警告中。只需将样式的jsx属性类型从boolean转换为string即可:

来自:<style jsx> {your style here} </style>

收件人:<style jsx="true"> {your style here} </style>

票数 4
EN

Stack Overflow用户

发布于 2021-12-16 03:54:47

如果任何人还在受苦,请尝试在styled-jsx文档中找到以下代码片段:

代码语言:javascript
复制
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://github.com/vercel/styled-jsx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57261540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档