gpt4 book ai didi

reactjs - 警告 : Received `true` for a non-boolean attribute `jsx` . 时代风格的 Jsx

转载 作者:行者123 更新时间:2023-12-04 11:54:30 27 4
gpt4 key购买 nike

我正在尝试将 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;

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

最佳答案

好的。由于我自己花了几个小时才解决这个问题,我希望我能帮助你们也有这个问题。
要将 styled-jsx 与 create-react-app 一起使用,您需要:

  • yarn add react-app-rewired customize-cra
  • 替换 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",
  • 创建(在根目录中,在 package.json 旁边)文件 config-overrides.js

  • const { 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 时才需要第 4 步和第 5 步或 yarn test使用创建 react 应用程序。

    关于reactjs - 警告 : Received `true` for a non-boolean attribute `jsx` . 时代风格的 Jsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261540/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com