Hướng dẫn import external css in reactjs - nhập css bên ngoài trong reactjs

Tôi phải thêm tệp CSS bên ngoài. Đã thử với nhập mã "./login.css"; nằm trong đường dẫn cơ sở. Không thể nhận được tệp, biến lỗi như dưới đây.

Bạn có thể cần một trình tải thích hợp để xử lý loại tệp này.

.Login {
         padding: 60px 0;
}

Tôi cũng đã cập nhật trong cấu hình webpack.

Cấu hình webpack:

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;

Trong tệp JSX:

import React from 'react';
import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap"; 
import "./Login.css";

Package.json,

{
  "name": "reactapp",
  "version": "1.0.0",
  "description": "Tetser",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.6.1",
    "react-bootstrap": "^0.32.1",
    "react-dom": "^15.6.1"
  }
}

Tôi đã thử tối đa tất cả mọi thứ, nhưng không có giải pháp. Bất cứ ai làm rõ, xin vui lòng.

Tổng quan

Trong ảnh này, chúng tôi sẽ đi qua những cách tốt nhất để thêm mã CSS vào React JS hoặc vào ứng dụng React. CSS rất quan trọng trong việc làm cho ứng dụng của bạn thân thiện với người dùng và hấp dẫn trực quan. Dưới đây là các cách khác nhau để thêm CSS vào ứng dụng React.

Biểu đồ bên ngoài

Bạn có thể tạo một tệp CSS mới trong thư mục dự án của mình và thêm CSS của bạn bên trong nó. Sau đó, bạn có thể nhập tệp vào trang thành phần, lớp hoặc React JS của bạn.

Bạn có thể sử dụng mã bên dưới để nhập bảng kiểu CSS bên ngoài:

import "./styles.css";

CSS nội tuyến

CSS nội tuyến có lẽ là phương pháp phổ biến nhất và nhanh nhất trong ba phương pháp để thêm CSS vào ứng dụng React. Tuy nhiên, nó có nhiều nhược điểm và thường không được khuyến khích sử dụng trừ khi bạn có một ứng dụng rất nhỏ. is probably the most common and quickest out of the three methods to add CSS to a React app. However, it has many disadvantages and is generally discouraged to use unless you have a very small application.

Chúng tôi tạo một đối tượng chứa các tài liệu tham khảo khác nhau, sau đó chúng tôi gọi với thuộc tính

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
2.

Ví dụ: CSS được thêm vào như hình dưới đây:

const styles = {
  section: {
    fontSize: "18px",
    color: "#292b2c",
    backgroundColor: "#fff",
    padding: "0 20px"
  },
  wrapper: {
    textAlign: "center",
    margin: "0 auto",
    marginTop: "50px"
  }
}

CSS sau đó được thêm vào một phần tử như vậy:

Các thành phần kiểu dáng

Các thành phần theo kiểu cho phép bạn viết CSS thực tế trong JavaScript của bạn. Ưu điểm chính là bạn có thể thêm mã có điều kiện và sử dụng các biến và chức năng trong CSS. let you write actual CSS in your JavaScript. The main advantage is that you can add conditional code and use variables and functions within the CSS.

Bạn có thể cài đặt

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
3 với lệnh sau:

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
4

Tiếp theo, bạn cần nhập

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
3 trong thành phần của mình. Sau đó, bạn có thể tạo một biến mới sẽ chứa CSS. Tên biến tương tự với dấu ngoặc mở và đóng sẽ hiển thị hoặc tạo phần tử HTML với các kiểu được thêm vào trước đó.

import styled from 'styled-components'
// Create a button variable and add CSS
const Button = styled.button`
  background: transparent;
  border-radius: 3px;
  border: 2px solid red;
  color:red;
`
//display the HTML
render(
  
);

Mô -đun CSS

Bạn cũng có thể thêm các kiểu vạch ra một cách dễ dàng, vì bạn chỉ cần tạo một tệp có phần mở rộng

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
6, như được hiển thị bên dưới:

// ComponentName.module.css

.Red {
  color: #f00;
}

.Blue {
  color: #00f;
}

Sau đó, bạn nhập các kiểu như sau:

import styles from "./ComponentName.module.css";

Bây giờ bạn có thể sử dụng các kiểu như hình dưới đây:

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
0

CSS nguyên tử

Một trong những khung CSS nguyên tử phổ biến nhất ngoài kia là đuôi. Nếu bạn bao gồm Tailwind trong dự án của mình bằng cách làm theo các hướng dẫn chính thức, bạn có thể sử dụng

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
7 mà không cần chạm vào CSS.

var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
      {
      test: /\.css$/,  
      include: /node_modules/,  
      loaders: ['style-loader', 'css-loader'],
 },
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;
1

Cảm xúc

Các thành phần kiểu dáng không phải là thư viện duy nhất cho phép bạn tạo các thành phần có kiểu nhúng. Một thay thế là cảm xúc. Điều tốt nhất về cảm xúc là khung của nó là bất khả tri, vì vậy bạn có thể đưa kiến ​​thức của mình đến các thư viện và khung khác ngoài phản ứng, trong khi khá giống với các thành phần kiểu dáng, vì vậy bạn có thể thay đổi nhập trong một số kịch bản).Emotion. The best thing about Emotion is that its framework is agnostic, so you can take your knowledge to other libraries and frameworks besides React, while being pretty similar to styled-components, so you can change the import in several scenarios).


Trong bức ảnh này, chúng tôi đã vượt qua các tùy chọn sẽ đánh dấu vào hầu hết các hộp cần thiết khi thêm CSS vào ứng dụng của bạn.

Người đóng góp

Saleh Mubashar

Làm cách nào để nhập mô -đun CSS vào React?

Để nhập mô -đun CSS vào thành phần tương ứng, hãy nhập bảng kiểu mô -đun CSS dưới dạng kiểu hoặc [Tên] Kiểu: Trong JSX, hãy sử dụng lớp CSS được xác định làm D như, chỉ cần nhớ nhập mỗi cái như một tên khác nhau.import the css modules stylesheet as styles or [name]Styles : In JSX, use the defined CSS class as a className prop like so: From here, you can add as many other CSS modules you'd like, just remember to import each as a different name.

Làm thế nào để bạn liên kết CSS với React JS?

Kiểu dáng phản ứng bằng CSS..
Chèn một đối tượng với thông tin kiểu dáng: lớp Myheader mở rộng React. ....
Sử dụng rentoundColor thay vì màu nền: lớp myheader mở rộng React.....
Tạo một đối tượng kiểu có tên Mystyle: Class Myheader mở rộng React ..

Tại sao CSS bên ngoài không hoạt động trong React?

Lỗi này được tạo vì trình biên dịch chỉ có thể nhập các tệp từ thư mục SRC.Ở đây, tệp CSS được lưu bên ngoài thư mục SRC, vì vậy trình biên dịch không thể nhập nó.Để làm cho mã này hoạt động, bạn chỉ cần lưu tệp CSS bên trong thư mục SRC.the compiler is only able to import files from the src folder. Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you just have to save the CSS file inside the src folder.

Tôi có thể nhập nhiều tệp CSS phản ứng không?

Bạn có thể nhập nhiều mô -đun CSS vào một thành phần hoặc chức năng bằng đối tượng.Gán ví dụ Nếu bạn nhập các mô -đun CSS nút vào thành phần demo của bạn, hãy thêm nó vào các kiểu mặc định của các thành phần.Sau đó, trong thành phần của bạn ... Bắt đầu sử dụng các kiểu CSS thuần túy.. assign For example if you import a button css modules to your Demo component, add this to the components default styles. Then in your component... start using pure css styles.