Why remote library works but local library throws error? - reactjs

I am building a component library, and install it as a local package, but it throws an error when I use react Hooks.
import React from "react";
import { useSelector } from "react-redux";
export default function Info() {
const name = useSelector((state) => state.account.name);
return (
<>
<ul>
<li>name: {name}</li>
</ul>
</>
);
}
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Then I upload this component library to remote, install it again, it solved.
But I still didn't know why...
Could anyone explain it?
My package.json
{
"name": "package",
"version": "0.1.0",
"private": true,
"main": "dist/index.js",
"babel": {
"presets": [
[
"react-app"
]
]
},
"dependencies": {},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"compile": "rm -rf dist && mkdir dist && cross-env NODE_ENV=development babel ./src/components -d dist --copy-files"
},
"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": {
"#babel/cli": "^7.12.0",
"cross-env": "^7.0.2",
"react-redux": "^7.2.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "3.4.3",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2"
},
}
Is there any difference behavior between the local package and remote package?
I read many articles, I guess it might be the multiple instances of React.
But why remote package wouldn't have this problem?
Articles I read:
Hooks + multiple instances of React #13991
Invalid Hook Call Warning

remove the brackets
const name = useSelector(state => state.account.name);

Add curly braces, and remove name in the end.. as you are trying to destructure the state.
const { name } = useSelector((state) => state.account);

Related

'useState' is defined but never used EVENTHOUGH I have used useState hook

I have simple functional component like below.
import React, { useState } from 'react';
import { connect } from 'react-redux';
import JoinRoomInputs from '../../../components/JoinRoomPage/JoinRoomInputs/JoinRoomInputs';
import { State } from '../../../store/states/states';
interface Props {
isRoomHost: boolean;
}
const JoinRoomContent = ({ isRoomHost }:Props) => {
const [roomIdValue, setRoomIdValue] = useState('');
const [nameValue, setNameValue] = useState('');
return (
<>
<JoinRoomInputs roomIdValue={roomIdValue} setRoomIdValue={setRoomIdValue} nameValue={nameValue} setNameValue={setNameValue} isRoomHost={isRoomHost} />
</>
);
};
const mapStateToProps = (state:State) => {
return {
...state
}
}
export default connect(mapStateToProps)(JoinRoomContent);
as you can see I have used useState hook in two lines.
But I'm getting yellow warning saying this.
src\components\JoinRoomPage\JoinRoomContent\JoinRoomContent.tsx
Line 1:17: 'useState' is defined but never used #typescript-eslint/no-unused-vars
Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.
And what is more curious is line 17 is this );
This doesn't make any sense to me.
What am I doing wrong here ?
below is my package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#reduxjs/toolkit": "^1.6.2",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"#types/jest": "^24.9.1",
"#types/node": "^12.20.36",
"#types/react": "^16.14.20",
"#types/react-dom": "^16.9.14",
"#types/react-redux": "^7.1.20",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-router-dom": "^6.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.1.6"
},
"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": {
"typescript-plugin-css-modules": "^3.4.0"
}
}
Restart your server or kill the port with the following command:
sudo kill $(sudo lsof -t -i:PORT)

How to import styles into React for Material Components for Web

I'm creating my own React components for Google's Material Components for Web. I'm trying to get the styling to work. According to the docs, you need to add the following to your sass file:
#use "#material/button";
// #include button.core-styles;
Note: I think there is a bug in the docs - button.core-styles doesn't exist. I just commented it out.
When the button renders, it has all the classes on the element, but all the styles associated with those classes are missing. So it doesn't look like an MDC button.
I found this bug report, which lead me to try this:
#import "#material/button/mdc-button";
And voila! The button is styled!
Can someone explain to me what the difference is between the #use and #import options? And why I would use one over the other?
Full example:
Button.scss
#import "~#material/button/mdc-button";
Button.js
import './Button.scss';
import {useEffect, useRef} from 'react';
import {MDCRipple} from '#material/ripple';
function Button(props) {
const buttonElement = useRef(null);
useEffect(() => {
const ripple = new MDCRipple(buttonElement.current);
return () => {
ripple.destroy();
}
});
return (
<button className="mdc-button mdc-button--raised" ref={buttonElement}>
<span className="mdc-button__ripple"></span>
<span className="mdc-button__label">{props.children}</span>
</button>
);
}
export default Button;
package.json
{
"name": "button-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material/button": "^12.0.0",
"#material/ripple": "^12.0.0",
"#material/theme": "^12.0.0",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"install": "^0.13.0",
"npm": "^7.20.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"sass": "^1.37.5",
"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"
]
}
}
Ugh. I just found this bug report. But the material.io website still has old info on it....
When I use:
#use "~#material/button/styles";
The button is now styled.

I am having a difficult time getting a simple React JS to start or run

I am having a difficult time getting a simple React JS to start or run. Her are my 2 simple files and package.json.
App.js
export default App;const App = () => {
return (
<div className="container">
<h1>Hello World</h1>
</div>
)
}
export default App
index.js
import { render } from ReactDOM
import App from './App'
render(<App />, document.getElementById('app'))
package.json
{
"name": "first-react-application",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.13.0",
"#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 have tried start index.js and start App.js. I don't get any errors server just won't run. I try to connect to 127.0.0.0:3000 and localhost:3000 and my browser never connects.
Any help would be greatly appreciated.
Thanks
Dawson
Get rid of the ‘ export default App;’ before the const in App.js

Why am I unable to import my component in react?

I am doing a basic import of a component and it keeps on giving me this error:
./src/Nav.js
Module not found: You attempted to import ..\node_modules\#pmmmwh\react-refresh-webpack-plugin\lib\runtime\RefreshUtils.js which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
But my Nav component is inside the /src.
my import
import React from 'react'
const Nav = () => {
return(
<header class="header">
</header>
)
}
export default Nav
Here is my package.json:
{
"name": "react-timesheet-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/core": "^7.12.16",
"#babel/preset-env": "^7.12.16",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-helmet": "^6.1.0",
"react-scripts": "4.0.2",
"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": {
"babel-loader": "^8.2.2",
"webpack": "^5.22.0"
}
}
I recently had a similar problem with missing packages, if that is the reason for the error, why do I keep on getting them?

React default create-react-app App component rendered twice

I started new React project via the command:
npx create-react-app my-app --template typescript
Then, I changed the App component to look like that:
import * as React from 'react';
export default class App extends React.Component<{}> {
public render() {
console.log("App Rendered");
return (
<div>
Hello World
</div>
);
}
}
and when I watch the Console log ( both in Google Chrome and Microsoft Edge ) I receive this messages:
[HMR] Waiting for update signal from WDS...
App Rendered
App Rendered
My Questions:
Why the App render is being called twice?
How to remove the first message with that WDS?
This is my package.json ( default one as I ran create-react-app ):
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"#types/jest": "^24.0.0",
"#types/node": "^12.0.0",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~3.7.2"
},
"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"
]
}
}
When wrapped with React.StrictMode (which is how CRA template are setup), on development, renders will be called twice. This is done to catch bugs that might occurs when some setState functions are not pure. You can read this for more detail.
Remove React.StrictMode from src/index.tsx

Resources