Home  >  Article  >  Web Front-end  >  What should I do if react cannot parse css?

What should I do if react cannot parse css?

藏色散人
藏色散人Original
2023-01-05 11:09:492125browse

React cannot parse css because when webpack configures the loader of the "css/less" file, modularization is not enabled by default or due to the incorrect introduction method. The solution: 1. Change the loader configuration of webpack ; 2. Introduce it through "@import '~antd/dist/antd.css';" in the "index.css" file.

What should I do if react cannot parse css?

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

What should I do if react cannot parse css?

  • #react Modular introduction of external css files does not take effect Problem handling

When introducing external css/less files into react, if you directly Import=》import “./search.less”;

Just use className directly when using it, as shown below:

What should I do if react cannot parse css?

But if you use this Import styles =》import styles from “./search.less”;

You will find that the styles do not take effect, as shown below:

What should I do if react cannot parse css?

The reason is, When webpack configures the loader of css/less files, modularization is not enabled by default. The introduction of this modular approach requires changing the loader configuration of webpack, as shown below:

What should I do if react cannot parse css?

  • Introducing antd style into React does not take effect?

When writing code, I think it is more convenient to use antd components (although antd is also very airtight)

This has happened before, because I downloaded antd The problem that arises is that the source using npm cannot be used in China. You can use Taobao's mirror source. If it still doesn't work, use the hotspot of your mobile phone to download it.

But this time there was a problem that the antd style did not take effect. I installed antd several times. I tried npm, cnpm, and yarn, but the problem still persisted. Nope

So I searched online, but none of the solutions seemed to be right. Then I went to the official website to check the introduction method

There is the following statement:

What should I do if react cannot parse css?

My code is written in other folders under scr, and the js file does not create a corresponding css file. There should be no need to introduce antd/dist/antd.css

So I thought it should be introduced in the css of the entry file index file, and it became

Solution:

1. Check whether antd is introduced successfully

Try a few more times. If there are too many warns, it means it is suspended

npm i antd
cnpm i antd
yarn add antd

2. Introduce in the index.css file:

@import '~antd/dist/antd.css';

Recommended learning: "react video tutorial"

The above is the detailed content of What should I do if react cannot parse css?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn