React Hook function useEffect - reactjs

Good Morning!
I'm starting react hooks, but it throws me the following error when wanting to use useEffect:
Line 4:5: React Hook "useEffect" is called in function "profile" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
The code is the following:
profile.js
import React, { useEffect } from 'react';
function profile(props) {
useEffect(() => {
document.title = props.nameAtributte;
}, [props.nameAtributte])
return (
<div style={{background:"yellow"}}>
It's my profile component {props.nameAtributte}
</div>
);
}
export default profile;
App.js
import React, {useState, useEffect} from 'react';
import Profile from './components/profile';
//import logo from './logo.svg';
//import './App.css';
function App() {
const [nombre, changeName] = useState('Juan Parez');
function nombreInput_onChanged(e){
changeName (e.target.value);
}
return (
<div className="App">
<h1>{nombre} eres digno, suficiente e ilimitado</h1>
<input type="text" name="nombreInput" id="nombreInput" value={nombre} onChange={nombreInput_onChanged} />
<Profile nameAtributte={nombre}/>
</div>
);
}
export default App;
package.json
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3"
},
What could be happening ?
I am reviewing some solutions here, but they did not work for me.
Thank you very much for the help.

Components name must start with a Capital Letter . As your profile components name starts with 'p' react is not counting it as a valid functional component.
Just change the component name to Profile and it should work fine

One of the rules of JSX is the name of the components must proceed with an uppercase letter.
If your JSX file contains a component that is having a lowercase letter as an initial letter then React will refer to it as a built-in component like <span> or <div> not a react component.
The Dot-notation component like <UserContext.Provider> and any component which starts with a capital letter indicates that the JSX tag is referring to a React component.
That's why you were getting the error as
React Hook "useEffect" is called in function "profile" which is neither a React function component or a custom React Hook function
So, in your case, you only need to change the function name as
function Profile(){
//your code
}
You don't have to change the name of the file i.e profile.js, there is no such rule for that.
You can see the Official React hooks rule book from here.

Related

Nextjs - Dynamic Imports - CSS Modules cannot be imported from within node_modules

Struggling to wrap my head around this issue. I am using the built in CSS modules for my components in Nextjs. When i lazy load my component that has a CSS module, I get the error CSS Modules cannot be imported from within node_modules..
If I replace ${asset.name} with the value button, i.e dynamic(() => import(#preamp/assets/Button)), nextjs will compile.
The code will execute correctly if i dont use CSS modules. It will lazy load dynamic components in. Only when i add the file to the project.
If i hardcode the import path it compiles. I only have this issue when I use a template literal string.
Any help is highly appreciated.
import React from 'react';
import dynamic from 'next/dynamic';
const asset = { name: 'Button' };
const NewComponent = dynamic(() => import(`~/assets/${asset.name}`), {
ssr: false,
});
export default function index() {
return (
<div className="grid-container">
<div className="grid-x grid-margin-x">
<div className="cell medium-6 large-6">
<NewComponent />
</div>
<div className="cell medium-6 large-6">12/6/8 cells</div>
</div>
</div>
);
}
Button.js
import React from 'react';
import styles from './Test.module.css';
export default function Index() {
return <div className={styles['btn-primary']}>Test Div</div>;
}
EDIT:
If I move the Test.module.css into a different folder, it will compile. I havent seen any documentation or reasoning why the CSS modules has to live in a particular area.
button.js updated
import React from 'react';
import styles from '~/test/Test.module.css'; // <-- Moved into a different folder
export default function Index() {
return <div className={styles['btn-primary']}>Test Div</div>;
}
Project specs:
"dependencies": {
"classnames": "^2.3.1",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-react": "^7.27.0",
"next": "^12.0.3",
"next-transpile-modules": "^9.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.43.4"
},
Using node v14.17.3
Just wanted to post my answer. If you are going to use dynamic imports, then import paths themselves cannot be dynamic. I've added more information here.
https://github.com/vercel/next.js/issues/31271

React component name must start with capital letter when using hooks

I'm trying to build my web-site with React.
I've learned that it's a good practice to name functional components starting with a capital letter, but presentational components name should start with a lowercase letter.
I try to follow this rule, but when I import any hook to presentational component named starting with lowercase, React says "Failed to compile".
Here is the message
Line 10:26: React Hook "useContext" is called in function "newCoring" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
I know that renaming component will help, but still wonder why it's "a good practice" to name presentational components this way when it does not work with React hooks?
Here is my component:
import React, {useContext} from 'react'
import InputComponent from '../Interface Components/InputComponent'
import SelectComponent from '../Interface Components/SelectComponent'
import AddToCart from '../Interface Components/AddToCart'
import DrawButton from '../Interface Components/DrawButton'
import Signs from '../Interface Components/Signs'
import Cog from '../Interface Components/Cog'
import InputContext from '../../context/input-context'
const newCoring = () => {
const inputContext = useContext(InputContext);
return (
<div className="first-line">
<Signs/>
<InputComponent id="width" default="500">Width:</InputComponent>
<InputComponent id="height" default="500">Height:</InputComponent>
<InputComponent id="depth" default="250">Depth:</InputComponent>
<SelectComponent id="diameter" default="152" values={inputContext.diameters}>Diameter:</SelectComponent>}
<Cog/>
<AddToCart/>
<DrawButton/>
</div>
);
}
export default newCoring;
but presentational components name should start with a lowercase letter
They aren't, User-Defined Components Must Be Capitalized
When an element type starts with a lowercase letter, it refers to a built-in component like <div> or <span> and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like <Foo /> compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

I get the Error message: "Invalid hook call" when I try to run my React app. Can't understand why

I get the error message: Invalid hook call. Hooks can only be called inside of the body of a function component when I try to run my react app.
import React from "react";
import BarChart from "../components/BarChart";
import LineChart from "../components/LineChart";
import Navbar from "../components/Navbar";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles({
row: {
display: "flex",
flexDirection: "row"
}
});
const Index = () => {
const classes = useStyles();
return (
<div>
<Navbar />
<div className={classes.row}>
<BarChart />
<LineChart />
</div>
</div>
);
};
export default Index;
It fails in line 16 (const classes = useStyles();).
The Navbar, BarChart and LinChart components is just components that I have created and it doesn't seem like the code breaks in these components. I'm able to run my code without any error messages if iI remove line 16.
Any ideas what might help?
I tried makeStyles locally and it worked perfectly fine. Kindly check your dependencies and update them to latest then try again.
I'm currently having:
"react": "^16.8.2", "react-dom": "^16.8.2", "#material-ui/core": "^4.9.12"

React Hooks Issue When Calling useState

I have the following React Todo component:
import React, { useState } from 'react';
const todo = props => {
const inputState = useState('') // problem with this line
return (
<React.Fragment>
<input type="text" />
</React.Fragment>
)
}
export default todo
When I run the app I get the following error in the browser:
./src/Todo.js
Line 5: React Hook "useState" is called in function "todo" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
I am using create-react-app to build my app and I am using the following versions of React.
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.0"
You need to write your component name with a capital first letter for it to be seen as a Component.
import React, { useState } from 'react';
const Todo = props => {
const inputState = useState('') // problem with this line
return (
<React.Fragment>
<input type="text" />
</React.Fragment>
)
}
export default Todo

How do I use checkbox from icheck plugin?

My code is :
import React, { Component } from 'react';
import { Card } from './Card';
import { Checkbox } from 'react-icheck';
class User extends Component {
render() {
return (
<Card>
<Checkbox
checkboxClass="icheckbox_square-blue"
increaseArea="20%"
label="Do you want to receive notification ?"
/>
</Card>
);
}
}
export default User;
Error : Expected a component class, got[object Object].
I don't know why I am getting errors for Checkbox Component.
version :
"icheck": "^1.0.2",
"react": "16.0.0-alpha.12",
"react-icheck": "^0.3.8",
"react-native": "0.47.1",
Can anyone say where I am doing wrong?
Can anyone say where I am doing wrong ?
You are trying to use DOM based Components inside a react-native application.
I don't think, that I should explain the difference

Resources