【问题标题】:Warning: Received `true` for a non-boolean attribute `jsx`. Zeit Styled Jsx警告:收到非布尔属性“jsx”的“真”。时代风格的 Jsx
【发布时间】: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;

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

【问题讨论】:

  • &lt;style jsx&gt; 上的 jsx 应该做什么?这就是它所抱怨的。
  • 这是一个 Babel 插件,可以让我使用 zeit/styled-jsx github.com/zeit/styled-jsx
  • 根据报错信息,你试过&lt;style jsx="true"&gt;吗?但根据文档,您不需要这样做。您是如何在App.js 中导入库的?
  • 将其设置为 true 会删除错误消息,但不会按预期工作。我不导入它,我只是将它包含在我的 package.json 作为插件中,这就是我想象它的工作方式。 React 不会加载我的 babel 插件有什么原因吗?
  • 可能 Babel 插件没有运行?

标签: reactjs ecmascript-6 babeljs styled-jsx


【解决方案1】:

好的。由于我自己花了几个小时才解决这个问题,我希望我能帮助你们也有这个问题。

要将styled-jsxcreate-react-app 一起使用,您需要:

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

  2. 替换成package.json:

"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",
  1. 创建(在根目录中,在package.json 旁边)文件config-overrides.js
const { override, addBabelPlugin, addBabelPreset } = require('customize-cra');
module.exports = override(
    addBabelPlugin('styled-jsx/babel')
);
  1. 在根目录下创建文件.babelrc(仅在运行jest test时使用)
{
    "plugins": ["styled-jsx/babel"]
}
  1. 在您的index.js(或者您将 React 挂载到 DOM 的位置)中,在调用 ReactDOM.render(...) 之前,插入以下代码,取自 styled-jsx issue #560
const _JSXStyle = require('styled-jsx/style').default;
if (typeof global !== 'undefined') {
    Object.assign(global, { _JSXStyle });
}

很遗憾,但是这个复杂的配置没有它就有点变幻莫测了。

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

【讨论】:

  • 这是取自这里的形式,在这里可以找到 npm codedaily.io/tutorials/… 的等效项
  • 如果你使用 nextjs,通过创建一个 .babelrc 你选择退出 rust 编译器 -_-'
【解决方案2】:

这个问题的答案在警告中。只需将样式的 jsx 属性类型从布尔值转换为字符串:

来自:&lt;style jsx&gt; {your style here} &lt;/style&gt;

收件人:&lt;style jsx="true"&gt; {your style here} &lt;/style&gt;

【讨论】:

  • 好人,请加分
【解决方案3】:

对于仍在受苦的人,请尝试在 styled-jsx 文档中找到的代码 sn-p:

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

【讨论】:

    【解决方案4】:

    使用样式化组件。不支持嵌套样式。看https://github.com/zeit/styled-jsx/pull/260 避免这种情况:

    <div>
      <span>
          <style jsx>{...}</style>
      </span>
    </div>
    

    【讨论】:

    • 您能否实现您在问题中链接到的代码并仅使用该链接作为参考?这是最好的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2023-02-09
    • 2022-10-13
    • 2020-08-12
    相关资源
    最近更新 更多