[英]React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
For the second day already I cannot understand what my mistake is, before I show the code, please look at the complete error第二天我已经无法理解我的错误是什么,在我显示代码之前,请查看完整的错误
I have studied various forums, including stackoverflow, but no advice has been able to resolve my error我研究了各种论坛,包括stackoverflow,但没有任何建议能够解决我的错误
I have three files TasksHoc, Introduction and Routes .我有三个文件TasksHoc, Introduction 和 Routes 。 I am importing a component from TasksHoc to Introduction and then to Routes
我正在将一个组件从 TasksHoc 导入到 Introduction 然后到 Routes
TasksHoc.js TasksHoc.js
const useStyles = makeStyles((theme) => ({
...code
}));
export function RadioButtonsHoc(...code) {
return function RadioButtonsGroup() {
...code
return (
...code
);
}
} }
Introduction.js介绍.js
import {RadioButtonsHoc} from "../Tasks/TasksHoc";
function Introduction(props) {
const {value} = props;
const [task, setTask] = useState([
{value: 'best', question: 'Question 1'},
{value: 'worst', question: 'Question 2'},
]);
return (
<>
{
task.map((i, index) => {
return(
<FormControlLabel value={i.value} control={<Radio/>} label={i.question}/>
);
}
)
}
</>
);
}
export const Introductions = RadioButtonsHoc(Introduction, 'value');
Routes.js路由.js
import {Introductions} from "../LessonComponents/Introduction";
function RoutesPage(props) {
const routes = [
{
path: `${path}/Introduction`,
component: () => <Introductions />
},
]
}
I tried different options export default import without curly braces and so on can't I missed something?我尝试了不同的选项导出默认导入没有大括号等等我不能错过什么吗? As for the paths of imports and exports of components, I use the WebStorm IDE, it automatically detects the paths
至于组件的导入导出路径,我用的是WebStorm IDE,它会自动检测路径
have you tried using class component instead of function component?您是否尝试过使用 class 组件而不是 function 组件?
class Introduction extends React.Component{
const {value} = props;
const [task, setTask] = useState([
{value: 'best', question: 'Question 1'},
{value: 'worst', question: 'Question 2'},
]);
render(){
return (
<>
{
task.map((i, index) => {
return(
<FormControlLabel value={i.value} control={<Radio/>} label={i.question}/>
);
}
)
}
</>
);
}
}
I changed the functional component RadioButtonGroup
to a class component and everything worked for me我将功能组件
RadioButtonGroup
更改为 class 组件,一切都对我有用
There is another case where this error might arise, and that is when your export is done incorrectly.在另一种情况下可能会出现此错误,即您的导出操作不正确。 If all of the JSX code that is to be returned by your functional component or by the render method of your component is stored in a javascript variable, and that variable is being returned directly, React treats the line as normal Javascript, as opposed to JSX.
如果您的功能组件或组件的渲染方法要返回的所有 JSX 代码都存储在 javascript 变量中,并且该变量被直接返回,那么 React 将该行视为普通 Javascript,而不是 JSX . This means that you shouldn't wrap the variable with curly braces.
这意味着您不应该用花括号将变量包裹起来。
if (props.show) {
bannerCode = (
<div className={classes.container}>
<h2>{props.text}</h2>
</div>
);
}
return {bannerCode};
The above code will throw the same type of error as the one in the question asked, because {bannerCode} is treated as an object.上面的代码将抛出与问题中相同类型的错误,因为 {bannerCode} 被视为 object。 In order to fix this, simply strip away the curly braces, like so.
为了解决这个问题,只需像这样去掉花括号。
if (props.show) {
bannerCode = (
<div className={classes.container}>
<h2>{props.text}</h2>
</div>
);
}
return bannerCode;
While this isn't the cause for error in the question's code snippet, I'm putting this here for reference purposes.虽然这不是问题代码片段出错的原因,但我将其放在这里以供参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.