【问题标题】:Unable to upload taiwind CSS in React project无法在 React 项目中上传顺风 CSS
【发布时间】:2022-01-07 15:39:10
【问题描述】:

我正在尝试在 react 项目中使用 Tailwind CSS。我按照here 的文档中给出的步骤进行操作。但在完成所有步骤后,我无法看到顺风 CSS 更改。 我正在像这样在文件Home.js 中添加样式,

import React from "react";
    .
    .
    .

  return (
    <>
        <div className="bg-red-500 h-96 py-80">
        <h1 className="text-3xl font-bold underline bg-yellow-400">
      Hello world!
    </h1>
        </div>
    </>
  );
};

export default Home;

但它没有显示任何内容 以下是所需文件:

package.json

{
  "name": "authlogin-boilerplate",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "express": "^4.17.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^6.1.1",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "autoprefixer": "^10.4.2",
    "postcss": "^8.4.5",
    "tailwindcss": "^3.0.11"
  }
}

tailwind.config.js

module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

App.js

import { useState } from "react";
import "./App.css";
import "./index.css";
import Navbar from "./components/Navbar";
import Home from "./components/Home";
import About from "./components/About";
import SignupPage from "./components/SignupPage";
import LoginPage from "./components/LoginPage";
import ForgotPasswordPage from "./components/ForgotPasswordPage";
import ChangePasswordPage from "./components/ChangePasswordPage";
import Alert from "./components/Alert";

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

function App() {
  const [alert, setAlert] = useState(null);

  const showAlert = (message, type) => {
    setAlert({ msg: message, type: type });
    setTimeout(() => setAlert(null), 1500);
  };

  return (
    <>
      <Router>
        <Navbar showAlert={showAlert} />
        <Alert alert={alert} />
        <Routes>
          <Route path="/" element={<Home showAlert={showAlert} />} />
         .
         .
      </Router>
    </>
  );
}

export default App;

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

另外,我的项目结构是

我注意到在我的 index.css 中,我收到了这个警告

不知道是什么原因,我已经重启了两次笔记本电脑,但还是不行。

【问题讨论】:

    标签: javascript css reactjs tailwind-css tailwind-ui


    【解决方案1】:

    这不起作用,因为您尚未将 CRACO 层添加到您的 React 应用程序中。您的设置很好,但仍有一些内容需要添加,您只需要在您的 react 应用根目录中运行 npm install @craco/craco 并使用新值 start、build 和弹出命令更改您的 package.json 文件,如下图所示,最后再次启动您的服务器,这将起作用。

    【讨论】:

    • 这是v,2,1,16 的文档,我正在使用v.3.0.11,因此在创建craco.config.js 后显示错误。 Error: PostCSS plugin tailwindcss requires PostCSS 8.
    • @MohitMaroliyaB17CS036 你需要安装 PostCSS 7 而不是 PostCSS 8 因为 create-react-app 不支持 PostCSS 8
    • 我只有 postCSS-7,错误提示它需要 postCSS -8
    猜你喜欢
    • 2020-09-02
    • 2021-11-12
    • 2021-06-05
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2021-07-15
    • 2021-10-29
    • 2018-08-31
    相关资源
    最近更新 更多