【问题标题】:Expo react-native use css-loaderExpo react-native 使用 css-loader
【发布时间】:2020-08-15 03:11:49
【问题描述】:

我对 Expo 和 react-native 很陌生。我在使用 css-loader 将样式加载为模块时遇到问题。我尝试了很多方法来使用Expo documentation 自定义 webpack.config.js,但它不起作用!

如果有人可以提供帮助,我将不胜感激,因为我在这个问题上花了很多时间......

重现问题:

expo init my-app

我的 webpack.config.js

const createExpoWebpackConfigAsync = require('@expo/webpack-config');

module.exports = async function (env, argv) {
    const config = await createExpoWebpackConfigAsync(
    {
        ...env,
        module: {
            rules: [
                    {
                        test: /\.(js|jsx)$/,
                        exclude: /node_modules/,
                        use: {
                            loader: "babel-loader"
                        }
                    },
                    {
                        test: /\.css$/,
                        loader: "style-loader"
                    },
                    {
                        test: /\.css$/,
                        loader: "css-loader",
                        options: {
                            modules: true,
                        }
                    }
                ],
            },
        extensions: [ '.web.js', '.js', '.jsx', '.css' ],
        },
    argv
    );
    return config;
};

我的组件:menu.jsx

import React from 'react';
import styles from './menu.module.css';
console.log(styles); // Displaying {}

class Menu extends React.Component {
    render() {
        return <div className={styles.red}>a</div>
    }
}

export default Menu;

我的风格:menu.module.css

.red {
    width: 300px;
    height: 300px;
    display: block;
    background-color: red;
}

【问题讨论】:

    标签: javascript react-native webpack expo


    【解决方案1】:
    • 很遗憾,css-loader 无法与 react-native/expo 一起使用。 有关样式的更多信息,请关注react-native docs

    如果你想写出像普通 css 一样的风格,看看styled-componenet

    • div 标签不能与 react-native 一起使用。替换中的 div react-native 我们有 View 和 Fragment。

    【讨论】:

    • expo 有没有 css-loader 的替代品?我正在寻找“react-native-css-transformer”你知道这是否有效吗?我试过了,我不能让它工作......
    • react-native-css-transformer 是 Metro 插件。它适用于裸反应原生。它不适用于世博会。
    • 为什么css加载器不能工作?虽然 RNW 组件使用自定义样式解决方案,但您仍然可以使用外部 css 和 className 为 HTML/React 组件设置样式
    猜你喜欢
    • 2021-10-18
    • 2021-05-06
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多