【问题标题】:Styled-jsx React nativeStyled-jsx React Native
【发布时间】:2019-10-09 15:55:17
【问题描述】:

我正在尝试在 React 本机项目中实现 styled-jsx。我正在关注本教程:https://github.com/zeit/styled-jsx

我已添加此代码:

.babelrc:

{
    "presets": [
        "@babel/preset-env"
    ],
    "plugins": [
        "@babel/plugin-proposal-object-rest-spread"
    ]
}

屏幕:

import React, { Component } from 'react';
import { View } from 'react-native';
import _JSXStyle from 'styled-jsx/style'
export default class HomeScreen extends React.Component {

    render() {
        return (
            <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>
        )
    };
}

但我收到以下错误:

============ 更新============

我从 'styled-jsx/style' 中删除了一个导入 _JSXStyle。

styled-jsx/babel:

module.exports = require('./dist/babel'),
    {
        "plugins": [
            ["styled-jsx/babel", { "optimizeForSpeed": false }]
        ]
    }

现在我收到此错误:

如果您尝试过,请提供建议。可能是我遗漏了任何文件,请告诉我。

谢谢!

【问题讨论】:

    标签: android reactjs react-native styled-components


    【解决方案1】:

    你的错误
    您尝试了 如何工作 部分的示例。库开发人员指出编译后的外观,所以,请按照文档。为了您更好地理解,我在这里给出。

    首先,安装包:

    npm install --save styled-jsx
    

    接下来,在你的 babel 配置中添加 styled-jsx/babelplugins

    {
      "plugins": [
        "styled-jsx/babel"
      ]
    }
    

    现在将 &lt;style jsx&gt; 添加到您的代码中并用 CSS 填充它:

    export default () => (
      <div>
        <p>only this paragraph will get the style :)</p>
    
        { /* you can include <Component />s here that include
             other <p>s that don't get unexpected styles! */ }
    
        <style jsx>{`
          p {
            color: red;
          }
        `}</style>
      </div>
    )
    

    注意:根据文档,它应该可以工作。 https://github.com/zeit/styled-jsx#getting-started

    【讨论】:

    • 你能告诉我在哪里可以找到这个 styled-jsx/babel 吗?目前我已经在 .babelrc 中添加了这个代码
    • 我仍然收到那个文件错误:(。你能解释一下哪个文件和哪个代码将在哪里。因为我第一次尝试这个
    • npm install --save styled-jsx 安装后你会在 node_modules 上得到它
    • 我有这个代码:module.exports = require('./dist/babel') 我应该在这个之后或任何数组中添加这个代码
    • 没有。它应该工作。如果没有,请重新运行您的项目。
    猜你喜欢
    • 2018-03-06
    • 2020-01-08
    • 2021-02-28
    • 2022-09-25
    • 1970-01-01
    • 2018-10-20
    • 2020-10-19
    • 2019-08-06
    • 2021-11-28
    相关资源
    最近更新 更多