gpt4 book ai didi

reactjs - 无法在 Storybook 中按类名设置我的 React 组件的样式

转载 作者:行者123 更新时间:2023-12-04 14:19:13 25 4
gpt4 key购买 nike

我无法通过类名在故事书中设置我的 React 组件的样式!

如果我使用像 div 这样的通用元素, <h1>等然后我可以设计一些东西。它必须与我的 webpack 配置有关,但我只是不知道是什么。

我的 rules.js get 传递到 webpack.config.js

rules.js

const { configureBabel } = require('../_babel.config');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');

/**
* @param themePaths {ThemePaths}
* @param selectMode {SelectMode}
* @param selectTarget {SelectTarget}
* @returns {*[]}
*/
function rules(themePaths, selectMode, selectTarget) {
const babelOptions = configureBabel(selectMode, selectTarget);
const babelEnvDeps = require('webpack-babel-env-deps');
return [
{
test: /\.graphql$/,
exclude: /node_modules/,
use: [
{
loader: 'graphql-tag/loader',
},
],
},
{
include: [
path.resolve(__dirname, '..', 'server'),
path.resolve(__dirname, '..', 'runtime'),
themePaths.src,
themePaths.generated,
path.resolve(__dirname, '..', '..', 'swagger'),
path.resolve(__dirname, '..', 'node_modules', 'debug'),
babelEnvDeps.include(),
],
// exclude: [/node_modules/],
test: /\.(ts|tsx|mjs|js|svg|graphql)$/,
use: [
{
loader: 'babel-loader',
options: babelOptions,
},
],
},
{
test: [/\.scss$/],
use: [
selectMode({ development: 'style-loader' }),
selectMode({ production: MiniCssExtractPlugin.loader }),
{
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: true,
},
},
{
loader: 'sass-loader',
options: {
data: `
@import "_vars";
@import "_mixins";
@import "_normalize";
@import "_reset";
`,
includePaths: ['src/scss'],
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [require('autoprefixer')()],
},
},
'resolve-url-loader',
].filter(Boolean),
},
{
test: /\.(jpe?g|png|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
{
test: /\.(woff2?)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
];
}

module.exports.rules = rules;

我的 webpack.config.js

const { resolve } = require('path');

module.exports = (baseConfig, env, defaultConfig) => {
const rulesFn = require('../.webpack/rules').rules;
const rules = rulesFn(
{
outputLegacy: '',
generated: '',
output: '',
src: '',
},
input => input['development'],
input => input['modern'],
);
delete rules[1].include;
defaultConfig.module.rules = rules;
defaultConfig.resolve.extensions = ['.ts', '.tsx', '.js'];
defaultConfig.resolve.alias = {
...(defaultConfig.resolve.alias || {}),
swagger: resolve(__dirname, '..', '..', 'swagger'),
};

return defaultConfig;
};

我的 config.ts

import { configure } from "@storybook/react";

const req = require.context('../src/', true, /.stories.tsx$/);

function loadStories() {
req.keys().forEach((filename) => req(filename))
}

configure(loadStories, module);

我的 React 组件 EateryItem.tsx

import React from "react";
import "./EateryItem.scss";

export const EateryItem: React.SFC<any> = props => {
return (
<div className="Eatery" key={props.id}>
<h3 className="name">{props.name}</h3>
<p className="address">{props.address}</p>
<p className="type">{props.foodType}</p>
</div>
);
};

我的 EateryItem.scss我似乎可以按类名来设置这些样式

.Eatery {
border: 5px dashed $color-primary;
margin: $base-spacing;
}

.name {
background: red;
}

不确定为什么我不能设置这些类名的样式?

最佳答案

因此,在检查我的故事书 webpack 中注入(inject)的 .js 文件中的类名后,我最终意识到它正在对类名进行哈希处理。

我进入我的 rules.js 并通过将其设置为 false 来关闭 css 模块。

loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: '[name]-[local]-[hash:base64:3]',
modules: false,
},

然后它停止散列 className 并开始工作!

关于reactjs - 无法在 Storybook 中按类名设置我的 React 组件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56840825/

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