我正在尝试在我的返回两个元素的座舱函数中使用useEffect,但是我收到这个奇怪的错误,说 “第6行:在函数“ cockpit”中调用了React Hook“ useEffect”,该函数既不是React函数的组件,也不是自定义的React Hook函数react-hooks / rules-of-hooks“。
但是我的座舱组件肯定是一个函数吗?
import React, { useEffect } from 'react'
import classes from './Cockpit.css'
const cockpit = (props) => {
useEffect(() => {
console.log('I work!')
})
const assignedClasses = []
let btnClass = ''
if (props.showPersons) {
btnClass = classes.Red;
}
if (props.persons.length <= 2) {
assignedClasses.push(classes.red)
}
if (props.persons.length <= 1) {
assignedClasses.push(classes.bold)
}
return (
<div className={classes.Cockpit}>
<h1>{props.title}</h1>
<p className={assignedClasses.join(' ')}>HELLO, HELLO!</p>
<button
className={btnClass}
onClick={props.clicked}>Click me!</button>
</div>
)
}
export default cockpit
答案 0 :(得分:4)
仅供参考,组件名称应以大写字母开头
答案 1 :(得分:2)
根据要响应的文档用户定义的组件必须大写!
如果仅使用react,则可以以小写字母定义组件函数名称,但是您注意到在渲染时使用组件时,您将其用作从'./ bla bla'。 因此,如果您确实有一个以小写字母开头的组件,请在使用JSX之前将其分配给大写的变量。
但是,如果您在该组件中使用带有react的钩子,则必须将组件大写。 我也看过Udemy课程。那个家伙对鸡尾酒命名组件很满意,但我认为它与react和hook的版本有关!
同样,我注意到您正在使用css模块。您是否设置了使用css模块作为课程的配置?
我必须提到,您可以使用css模块而无需运行'npm run弹出',而这些复杂的配置作为React 2支持css模块,您只需要将扩展名从.css
更改为.module.css
这是您的干净代码:
import React, { useEffect } from 'react'
import classes from './Cockpit.module.css'
const Cockpit = (props) => {
useEffect(() => {
console.log('I work!')
})
const assignedClasses = []
let btnClass = ''
if (props.showPersons) {
btnClass = classes.Red;
}
if (props.persons.length <= 2) {
assignedClasses.push(classes.red)
}
if (props.persons.length <= 1) {
assignedClasses.push(classes.bold)
}
return (
<div className={classes.Cockpit}>
<h1>{props.title}</h1>
<p className={assignedClasses.join(' ')}>HELLO, HELLO!</p>
<button
className={btnClass}
onClick={props.clicked}>Click me!</button>
</div>
)
}
export default Cockpit
答案 2 :(得分:1)
//This is the exact solution
import React, { useEffect } from 'react';
import classes from './Cockpit.css';
const Cockpit = props => {
useEffect(() => {
console.log('[Cockpit.js] useEffect');
// Http request...
setTimeout(() => {
alert('Saved data to cloud!');
}, 1000);
return () => {
console.log('[Cockpit.js] cleanup work in useEffect');
};
}, []);
useEffect(() => {
console.log('[Cockpit.js] 2nd useEffect');
return () => {
console.log('[Cockpit.js] cleanup work in 2nd useEffect');
};
});
// useEffect();
const assignedClasses = [];
let btnClass = '';
if (props.showPersons) {
btnClass = classes.Red;
}
if (props.persons.length <= 2) {
assignedClasses.push(classes.red); // classes = ['red']
}
if (props.persons.length <= 1) {
assignedClasses.push(classes.bold); // classes = ['red', 'bold']
}
return (
<div className={classes.Cockpit}>
<h1>{props.title}</h1>
<p className={assignedClasses.join(' ')}>This is really working!</p>
<button className={btnClass} onClick={props.clicked}>
Toggle Persons
</button>
</div>
);
};
export default Cockpit;
// Look out for the identifier names of the const and the export
答案 3 :(得分:0)
答案 4 :(得分:0)
import React, { useEffect, useRef, useContext } from 'react';
import classes from './Cockpit.css';
import AuthContext from '../../context/auth-context';
const Cockpit = props => {
const toggleBtnRef = useRef(null);
const authContext = useContext(AuthContext);
console.log(authContext.authenticated);
useEffect(() => {
console.log('[Cockpit.js] useEffect');
// Http request...
// setTimeout(() => {
// alert('Saved data to cloud!');
// }, 1000);
toggleBtnRef.current.click();
return () => {
console.log('[Cockpit.js] cleanup work in useEffect');
};
}, []);
useEffect(() => {
console.log('[Cockpit.js] 2nd useEffect');
return () => {
console.log('[Cockpit.js] cleanup work in 2nd useEffect');
};
});
// useEffect();
const assignedClasses = [];
let btnClass = '';
if (props.showPersons) {
btnClass = classes.Red;
}
if (props.personsLength <= 2) {
assignedClasses.push(classes.red); // classes = ['red']
}
if (props.personsLength <= 1) {
assignedClasses.push(classes.bold); // classes = ['red', 'bold']
}
return (
<div className={classes.Cockpit}>
<h1>{props.title}</h1>
<p className={assignedClasses.join(' ')}>This is really working!</p>
<button ref={toggleBtnRef} className={btnClass} onClick={props.clicked}>
Toggle Persons
</button><hr/>
<button onClick={authContext.login}>Log in</button>
</div>
);
};
export default React.memo(Cockpit);
只需删除我在此答案中给出的驾驶舱组件代码和副本