Material UI Button is causing Invalid Hook Call - reactjs

I'm a beginner at React app, I just finished setting up the app and want to use material UI for buttons.
Here's my code.
import Button from '#material-ui/core/Button';
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className = "App">
<header className='App-header'>
<Button>Click here</Button>
<img src={logo} className="App-logo" alt="logo" />
</header>
</div>
);
}
export default App;
I run that and it gives me Error: Invalid hook call. Hooks can only be called inside of the body of a function component. and the error points to:
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
> 7 | ReactDOM.render(
8 | <React.StrictMode>
9 | <App />
10 | </React.StrictMode>,
This is my package.json just in case, but I don't think there's anything wrong there.
{
"name": "schedule",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"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"
]
}
}
I've spent so much time trying to fix this, please help

I have reproduced your code in this example
https://codesandbox.io/embed/dreamy-euler-jhu8t?fontsize=14&hidenavigation=1&theme=dark
Need only install material ui with
// with npm
npm install #material-ui/core
// with yarn
yarn add #material-ui/core
And delete if no need
from './reportWebVitals'

I just needed to do npm install #material-ui/core in the terminal. It wasn't in the dependencies list before. Thanks to #Claire Lin.

Related

Issue with .env being undefinde when using create react app --template typescript?

I am having an issue defining env file in react app using the npx create-react-app my-app --template typescript, below are all the needed info on how the structure is shouldn't the env file work right out of the box ? or is this some new issue ? , basically I didn't touch any think after creating the app just put the console log in the app js so I could see the behavour.
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
console.log(process.env);
return (
<div className='App'>
<header className='App-header'>
<img src={logo} className='App-logo' alt='logo' />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a className='App-link' href='https://reactjs.org' target='_blank' rel='noopener noreferrer'>
Learn React
</a>
</header>
</div>
);
}
export default App;
// .env file
REACT_APP_PAYPAL_CLIENT_ID : AWLe2ytIee-lQTYPM_1v1fSnXxiJj-xDgr0xngrbX2vIDNA0zPw_028LsGlLBStEeHhTFzsnLjvqIPij
// Package
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.4.0",
"#types/node": "^16.11.21",
"#types/react": "^17.0.38",
"#types/react-dom": "^17.0.11",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"typescript": "^4.5.5",
"web-vitals": "^2.1.3"
},
"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"
]
}
}
From the Adding Development Environment Variables In .env,
The format should be key=value, NOT key: value. Replace : to =
.env:
REACT_APP_PAYPAL_CLIENT_ID=AWLe2ytIee-lQTYPM_1v1fSnXxiJj-xDgr0xngrbX2vIDNA0zPw_028LsGlLBStEeHhTFzsnLjvqIPij

index.js not uploading to github, keeps getting ignored

created a new project by using npx create-react-app newproject. Index.js inside /src keeps getting ignored when trying to upload it to github. Everything gets uploaded correctly. Is this a problem with webpack, any dependencies? This is unusual.
folders and such:
index.js:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
App.js:
import logo from "./logo.svg";
import "./App.css";
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
.gitignore:
node_modules
build
package.json:
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.3"
},
"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": {
"mini-css-extract-plugin": "2.4.5"
}
}
Uploading process: git add . && git commit -m "commit message here" && git push origin main

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.)

React bulma components import is not working

I have created the react app using CRA (CREATE-REACT-APP). I have installed node-sass and renamed App.css to App.scss and updated its import in App.js. Now in my App.js, I am just trying to print some text inside a section.
This is the link the repo, I am talking about. For your reference
node -v v12.11.1 and npm -v 6.11.3
App.js
import React from 'react';
import './App.scss';
import Section from 'react-bulma-components/lib/components/section';
function App() {
return (
<Section>
<div class="container">
<h1 class="title">
Hello World
</h1>
<p class="subtitle">
My first website with <strong>Bulma</strong>!
</p>
</div>
</Section>
);
}
export default App;
App.scss
#import 'react-bulma-components/src/index.sass';
package.json
{
"name": "just-trying",
"version": "0.1.0",
"private": true,
"dependencies": {
"node-sass": "^4.13.0",
"react": "^16.11.0",
"react-bulma-components": "^3.1.3",
"react-dom": "^16.11.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "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": {
"sass-loader": "^8.0.0"
}
}
Instead of downgrading the version of node, I have uninstalled react-bulma-components and instead just installed bulma via npa like npm i bulma.. and that works fine.

Resources