简体   繁体   English

使用 styled-jsx 而不弹出 create-react-app

[英]Use styled-jsx without ejecting create-react-app

Since I started using NextJS, I've grown quite fond of styled-jsx (I know, not everyone likes it).自从我开始使用 NextJS,我就非常喜欢 styled-jsx(我知道,不是每个人都喜欢它)。 I'd love to use it in my create react app.我很想在我的 create react 应用程序中使用它。 Locally it works great.在本地效果很好。 However, react shows me the error:但是,react 向我显示了错误:

Warning: Received `true` for a non-boolean attribute `jsx`.

To my understanding, this means that the code does not get transpired by babel.据我了解,这意味着代码不会被 babel 执行。 I would need to add the babel plugin to my Create React App Babel config - which isn't possible without ejecting.我需要将 babel 插件添加到我的 Create React App Babel 配置中 - 如果不弹出,这是不可能的。

I've also read about react-app-rewired but I don't trust it enough to put into production.我也读过关于react-app-rewired但我不太相信它可以投入生产。 Is there a more native way to use styled-jsx in my create react app project?在我的 create react app 项目中是否有更原生的方式来使用styled-jsx

According to the styled-jsx docs, it can be used in create-react-app by using the css.resolve function provided in styled-jsx/macro .根据 styled-jsx 文档,它可以通过使用styled-jsx/macro提供的css.resolve函数在create-react-app使用。 Read about it here . 在这里阅读。

I think this is the intended use but I could not get it working:认为这是预期用途,但我无法使其正常工作:

import css from "styled-jsx/macro";

export function Login() {
    const { demoClass, styles } = css.resolve`
        label {
            color: green;
        }
    `;

    return (
        <label className={demoClass}>Test</label>
    );
}

Even if this did work, I dislike it and would rather use styled components or CSS modules (built into CRA, by the way).即使这确实有效,我也不喜欢它,宁愿使用样式组件或 CSS 模块(顺便说一下,内置于 CRA)。 This is nowhere near as clean as the normal styled-jsx code.这远不如普通的 styled-jsx 代码干净。

It seems that styled-jsx just does not work well with CRA without ejecting.似乎 styled-jsx 在没有弹出的情况下不能很好地与 CRA 配合使用。 If you do get it working please let me know, but right now I am going down the CSS modules with Styled Components route.如果你真的让它工作了,请告诉我,但现在我正在使用样式化组件路线向下浏览 CSS 模块。

我碰巧在另一个问题下详细回答了这个问题:) 希望它有帮助https://stackoverflow.com/a/66285652/511908

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM