Tailwind CSS 3 is not being applied in my React app - reactjs

I want to use Tailwind CSS in my React app, but it is not working. I followed the description on the Tailwind site for how to install it, but the CSS is not being applied.
App.js
// import './App.css';
import './index.css';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Home from './Home/Home'
function App() {
return (
<Router>
<Routes>
<Route>
<Route exact path="/" element={<Home/>} />
</Route>
</Routes>
</Router> );
}
export default App;
index.css
#tailwind base;
#tailwind components;
#tailwind utilities;
tailwind.config
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
"./public/index.html",
],
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
package.json
{
"name": "archives",
"version": "0.1.0",
"private": true,
"dependencies": {
"#tailwindcss/postcss7-compat": "^2.2.17",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"nanoid": "^4.0.0",
"picocolors": "^1.0.0",
"postcss-loader": "^7.0.1",
"postcss-preset-env": "^7.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"source-map-js": "^1.0.2",
"web-vitals": "^2.1.4"
},
"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": "^9.8.8",
"postcss": "^7.0.39",
"postcss-flexbugs-fixes": "^5.0.2",
"tailwindcss": "^3.1.6"
}
}
Home.js
import React from 'react';
import '../index.css';
const Home = () => {
return (
<div className='w-full h-full'>
<div className='flex justify-center'>
<h1 className=
'text-2xl lg:text-5xl font-bold text-green-600 mt-12'>
hi</h1>
</div>
</div>
);
};
export default Home;

My tailwind is also broken, however, a few weeks ago I ran into the same issue.
The solution that worked for me was to add in any vanilla css to my index.css like so:
#tailwind base;
#tailwind components;
#tailwind utilities;
body {
background-color: #3f4040;`
}
(Sorry if this isn't a great post. It's my first!)

Related

How to add SVG from Fontawesome react code snippet in React app?

I am using CSS module. Does it affect in how to add the Font awesome icon?
I already installed all the required dependencies.
nav.js
import React from "react";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
import classes from "./Nav.module.css";
import Button from "../UI/Button/Button";
const Nav = (props) => {
return (
<section className={classes.nav}>
<h1>ReactMeals</h1>
<Button className={classes["nav--btn"]}>
<FontAwesomeIcon icon={["fa-solid", "fa-cart-shopping"]} />
Your Cart
<span className={classes["num__items"]}>0</span>
</Button>
</section>
);
};
export default Nav;
package.json
{
"name": "foodorderapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.2.1",
"#fortawesome/free-regular-svg-icons": "^6.2.1",
"#fortawesome/free-solid-svg-icons": "^6.2.1",
"#fortawesome/react-fontawesome": "^0.2.0",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"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"
]
}
}
Tried to through Font awesome documentation. But it didn't work.

ReactJS - Uncaught TypeError: Cannot set property default of #<Object> which has only a getter

Image of Error
Facing an error while coding using React.js. I have know idea about it. Hence i don't know how to ask this question. Besides, I have attached an image of an error file. Putting the App.js, Header.js and package.json files. I have been trying to resolve the issue for an hour but couldn't get the success. Please help.
App.js
import { Fragment } from "react";
import Header from "./components/Layout/Header";
function App() {
return (
<Fragment>
<Header/>
</Fragment>
);
}
export default App;
Header.js
import React, {Fragment} from 'react'
import mealsImage from '../../assets/meals.jpg'
import classes from './Header.module.css'
const Header = (props) => {
return (
<Fragment>
<header className={classes.header}>
<h1>ReactMeals</h1>
<button>Cart</button>
</header>
<div className={classes=['main-image']}>
<img src={mealsImage} alt='A Table full of delicious meals' />
</div>
</Fragment>
)
}
export default Header
Package.json
{
"name": "react-complete-guide",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "^5.0.1",
"web-vitals": "^2.1.4"
},
"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"
]
}
}
Try to change the className of the img container to:
<div className={classes['main-image']}>
You have issue in Header.js file, let me provide you fix code
import React, {Fragment} from 'react'
import mealsImage from '../../assets/meals.jpg'
import classes from './Header.module.css'
const Header = (props) => {
return (
<Fragment>
<header className={classes.header}>
<h1>ReactMeals</h1>
<button>Cart</button>
</header>
<div className={classes['main-image']}> {/* issue was here you were using classes=['main-image'], you do not need = here */}
<img src={mealsImage} alt='A Table full of delicious meals' />
</div>
</Fragment>
)
}
export default Header

Module not found: Error: Package path . is not exported from package react

I am getting following error in my react application. This issue started when I renamed some components and deleted some. However I look closely and imports and component names are correct. I did a fresh checkout, renamed one component and got the same issue.
ERROR in ./src/components/courses/CourseList.js 7:0-40
Module not found: Error: Package path . is not exported from package ....../view/node_modules/#types/react (see exports field in ....../view/node_modules/#types/react/package.json)
ERROR in ./src/components/topics/TopicListDetail.js 7:0-51
Module not found: Error: Package path . is not exported from package ...../view/node_modules/#types/react (see exports field in ...../view/node_modules/#types/react/package.json)
both components are only imported in one place
import TopicList from "../../components/topics/TopicList";
import CourseList from "../../components/courses/CourseList";
Both follow the same conventions
function TopicList() {
let topicList = [];
for(let i = 0; i < topicList.length; i++) {
topicList.push(
<NavLink to={"/topic/" + i} className="" key={"popular-topic-" + i}>
<div className="">
<img src={"http://placeimg.com/100/100/people?" + i}
className="rounded-full hover:bg-sky-700" alt={"topic" + i} />
</div>
</NavLink>
);
}
return (
<div className="">
pretty tailwind css
</div>
)
}
export default TopicList;
I deleted my node_modules and package-lock.json and ran npm i. I am not seeing any errors in the js. I am not sure where to look at this point.
Here are where they are imported
import TopicList from "../../components/topics/TopicList";
import CourseList from "../../components/courses/CourseList";
function Home() {
return (
<Fragment>
<div className="box-border hover:box-content max-width: 640px; ">
<TopicList/>
<CourseList/>
</div>
</Fragment>
)
}
export default Home;
{
"name": "view",
"version": "0.1.0",
"private": true,
"dependencies": {
"#popperjs/core": "^2.11.5",
"#react-buddy/ide-toolbox": "^2.1.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.1.1",
"#testing-library/user-event": "^13.5.0",
"#types/react": "^18.0.10",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-minimal-side-navigation": "^1.9.2",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"redux-thunk": "^2.4.1",
"web-vitals": "^2.1.4"
},
"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": {
"#tailwindcss/typography": "^0.5.2",
"autoprefixer": "^10.4.7",
"postcss": "^8.4.13",
"tailwindcss": "^3.0.24"
}
}
advice?
The issue for me ended up being image imports in the js file.
I had these mock valus:
import Course1 from "../../ui/course-1.png";
const [popularCourse] = useState([
{
ID: 1,
title: "Learning How to Create Beautiful Scenes in Illustrator in 60 minutes",
tutor: {
ID: 1,
name: "Lana Marandina",
username: "lanamara",
dp: "http://placeimg.com/100/100/people?tutor-" + 1,
},
duration: "82 min",
poster: Course1
},
Then add them to a list
var courseList = [];
for(let i = 0; i < popularCourse.length; i++){
courseList.push(
<NavLink to={"/course/" + popularCourse[i].ID} className="" key={"popular-course-" + i}>
<div className="rounded-lg p-2" style={{
background: "#e2e2e2 url(" + popularCourse[i].poster +") no-repeat center"
}}>
<div className="h-56">
<div className="">
<img src={popularCourse[i].tutor.dp} className="rounded-full" alt="popular-course"/>
</div>
</div>
The array path for poster was wrong, but the IDE gave me a very unhelpful error.

Invalid Hook Call while trying to use Material UI

I was trying to add search icon from material UI but I am getting this error. "Error: Invalid hook call. Hooks can only be called inside of the body of a function component." I was trying the amazon clone tutorial from YouTube and did everything exactly. Can someone help me? Thank you.
This is my Header.js
import React from "react";
import "./Header.css";
import { Link } from "react-router-dom";
import SearchIcon from '#material-ui/icons/Search';
function Header() {
return (
<nav className="header">
{/* Logo on the left -> img */}
<Link to="/">
<img className="header__logo" src="/logo.png"></img>
</Link>
{/* Search box */}
<input type="text" className="header__serachInput"/>
<SearchIcon />
{/* Links */}
{/* Basket icon with number */}
</nav>
)
}
export default Header
Package.json
{
"name": "amazon-clone",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.11.2",
"#material-ui/icons": "^4.11.2",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"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"
]
}
}
Your code looks pretty fine and I don't see the use of hooks in your code here.
Try updating your react-dom module.
See the update error here

React - Module not found: Can't resolve '#bootstrap-styled/v4'

I wonder why React can't find '#bootstrap-styled/v4'.
At first, I didn't eject. Because it's my first React project. I want simple.
yarn add '#bootstrap-styled/v4'
So this package installed in 'Project/node_modules/#bootstrap-styled/v4'.
And package.json file here.
# package.json
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
"#bootstrap-styled/v4": "bootstrap-styled/v4",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"styled-components": "^5.1.1"
},
"scripts": {
"start": "cross-env NODE_PATH=src react-scripts start",
"build": "cross-env NODE_PATH=src react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"cross-env": "^7.0.2"
}
}
# src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import 'index.css';
import BaseLayout from 'BaseLayout';
import * as serviceWorker from 'serviceWorker';
ReactDOM.render(
<React.StrictMode>
<BaseLayout />
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
# src/BaseLayout.js
import React, { useState } from 'react';
import {
Nav,
NavItem,
NavLink,
NavDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem,
} from '#bootstrap-styled/v4';
// root component
function BaseLayout() {
const [isOpen, setIsOpen] = useState(false);
return (
<Nav>
<NavItem>
<NavLink href="">Home</NavLink>
</NavItem>
<NavDropdown isOpen={isOpen} toggle={() => setIsOpen(true)}>
<DropdownToggle>
General
</DropdownToggle>
<DropdownMenu>
<DropdownItem>1</DropdownItem>
<DropdownItem>2</DropdownItem>
<DropdownItem>3</DropdownItem>
</DropdownMenu>
</NavDropdown>
</Nav>
);
}
export default BaseLayout;
I ran the server, error displayed.
Module not found: Can't resolve '#bootstrap-styled/v4' in 'D:\Project\src'
I have a few questions here.
I heard if you don't eject, React project will find module in src only. Is that right?
1-1. If correct, I don't make sense. Then why does node_modules exist? It would be right to go inside src, right?
What mean '#'? This isn't a scoped package, is it? Cause scoped package's structure is #scope/packageName. '#bootstrap-styled/v4' does not apply. (At https://bootstrap-styled.github.io/bootstrap-styled/, you can see their packages.)

Resources