简体   繁体   English

无法渲染 react-router-dom 基本功能组件

[英]Can't render a react-router-dom basic functional component

i'm new to react and started working on a Single page web app with nodejs backend and a simple react with react-bootstrap and react-router-dom front-end, i've learned Functional components and Class Components, everything worked fine for me, until i got this strange error when i try to render a Functional component.我是新手,开始使用带有 nodejs 后端的单页 web 应用程序以及与 react-bootstrap 和 react-router-dom 前端的简单反应,我已经学习了功能组件和 Class 组件,一切正常我,直到我尝试渲染功能组件时遇到这个奇怪的错误。

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

The error is triggered when i try to render this functional component.当我尝试渲染此功能组件时触发错误。

 import React, { useState } from 'react' import { Row, Col, Form } from 'react-bootstrap'; export default (props) => { const [clans, setClans] = useState([]); return(<> <div> <Row className="pt-4"> <Col md={12}> <Form.Input type="text" placeholder="Search" /> </Col> </Row> </div> </>); }

and here is where i call for it ( component={Clanes} )这就是我需要它的地方( component={Clanes}

 import React from 'react' import { Route, Switch } from 'react-router-dom'; //paginas import Home from './page/Home'; import NuevoClan from './page/NuevoClan'; import Registrarse from './page/Registrarse'; import Clanes from './page/Clanes'; export default ()=>{ return (<> <Switch> <Route exact={true} path='/' component={Home}/> <Route path='/clanes' component={Clanes}/> // this route can't be rendered <Route path='/nuevoclan' component={NuevoClan}/> <Route path='/registrarse/:clan?' component={Registrarse}/> </Switch> </>); }

It should be noted that I have other functional components in the project with the same structure that render without problems.需要注意的是,我在项目中还有其他功能组件具有相同的结构,可以毫无问题地渲染。 NuevoClan & Registrarse uses the same functional component structure. NuevoClan & Registrarse使用相同的功能组件结构。 Home is a Class component. Home是一个 Class 组件。

i started thinking is a cache problem but i can't find anything related to react cache cleaning我开始认为是缓存问题,但我找不到与反应缓存清理相关的任何内容

I would triple check this line我会三重检查这条线

import Clanes from './page/Clanes';

Also check the file name of Clanes and the extension.还要检查 Clane 的文件名和扩展名。 Make sure there isn't a space in the name.确保名称中没有空格。

Note: The fragment around the div is unnecessary.注意:div 周围的片段是不必要的。

Ok, i feel ashamed.好吧,我感到很惭愧。 The problem was the component Form.Input does not exists.问题是组件 Form.Input 不存在。 so it couldn't be rendered and passed to the Route.所以它不能被渲染并传递给路由。 then i replaced <Form.Input type="text" placeholder="Search" /> to <Form.Control type="text" placeholder="Search" /> and everything was solved然后我将<Form.Input type="text" placeholder="Search" />替换为<Form.Control type="text" placeholder="Search" />一切都解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM