Javascript 元素类型无效React和React引导
我正在使用React和React引导 以下是我创建组件的代码:Javascript 元素类型无效React和React引导,javascript,reactjs,twitter-bootstrap,bootstrap-4,jsx,Javascript,Reactjs,Twitter Bootstrap,Bootstrap 4,Jsx,我正在使用React和React引导 以下是我创建组件的代码: export const CollapsableNavItem = (props) => { const { pathname } = useLocation(); const { eventKey, title, icon, children = null } = props; const defaultKey = pathname.indexOf(eventKey) !== -1 ? eventKey : &q
export const CollapsableNavItem = (props) => {
const { pathname } = useLocation();
const { eventKey, title, icon, children = null } = props;
const defaultKey = pathname.indexOf(eventKey) !== -1 ? eventKey : "";
return (
<Accordion as={Nav.Item} defaultActiveKey={defaultKey}>
<Accordion.Item eventKey={eventKey}>
<Accordion.Button as={Nav.Link} className="d-flex justify-content-between align-items-center">
<span>
<span className="sidebar-icon"><FontAwesomeIcon icon={icon} /> </span>
<span className="sidebar-text">{title}</span>
</span>
</Accordion.Button>
<Accordion.Body className="multi-level">
<Nav className="flex-column">
{children}
</Nav>
</Accordion.Body>
</Accordion.Item>
</Accordion>
);
};
export default (props = {}) => {
const location = useLocation();
const { pathname } = location;
const [show, setShow] = useState(false);
const showClass = show ? "show" : "";
const onCollapse = () => setShow(!show);
const NavItem = (props) => {
const { title, link, external, target, icon, image, badgeText, badgeBg = "secondary", badgeColor = "primary" } = props;
const classNames = badgeText ? "d-flex justify-content-start align-items-center justify-content-between" : "";
const navItemClassName = link === pathname ? "active" : "";
const linkProps = external ? { href: link } : { as: Link, to: link };
return (
<Nav.Item className={navItemClassName} onClick={() => setShow(false)}>
<Nav.Link {...linkProps} target={target} className={classNames}>
<span>
{icon ? <span className="sidebar-icon"><FontAwesomeIcon icon={icon} /> </span> : null}
{image ? <Image src={image} width={20} height={20} className="sidebar-icon svg-icon" /> : null}
<span className="sidebar-text">{title}</span>
</span>
{badgeText ? (
<Badge pill bg={badgeBg} text={badgeColor} className="badge-md notification-count ms-2">{badgeText}</Badge>
) : null}
</Nav.Link>
</Nav.Item>
);
};
为什么会出现上述错误
我改变的是:
我在导出默认值中使用了“CollapsableNavItem”方法,但是现在我更改了它,现在在外部使用了它,但是仍然会遇到相同的错误。如果您正在创建组件
const CollapsableNavItem = (props) => {
const { eventKey, title, icon, children = null } = props;
const defaultKey = pathname.indexOf(eventKey) !== -1 ? eventKey : "";
return (
<Accordion as={Nav.Item} defaultActiveKey={defaultKey}>
<Accordion.Item eventKey={eventKey}>
<Accordion.Button as={Nav.Link} className="d-flex justify-content-between align-items-center">
<span>
<span className="sidebar-icon"><FontAwesomeIcon icon={icon} /> </span>
<span className="sidebar-text">{title}</span>
</span>
</Accordion.Button>
<Accordion.Body className="multi-level">
<Nav className="flex-column">
{children}
</Nav>
</Accordion.Body>
</Accordion.Item>
</Accordion>
);
const CollapsableNavItem=(道具)=>{
const{eventKey,title,icon,children=null}=props;
const defaultKey=pathname.indexOf(eventKey)!=-1?eventKey:;
返回(
{title}
{儿童}
);
我使用的是导出默认值,所以我可以在其他模块中使用它。你认为呢?我想说的是,你需要让CollapsableNavItem成为一个独立的组件。一旦你将它移出,如果你使用的是导出常量CollapsableNavItem
,那么你需要使用导入{CollapsableNavItem}从“文件/路径”开始
同样适用于NavItem
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.
Check the render method of `CollapsableNavItem`.
const CollapsableNavItem = (props) => {
const { eventKey, title, icon, children = null } = props;
const defaultKey = pathname.indexOf(eventKey) !== -1 ? eventKey : "";
return (
<Accordion as={Nav.Item} defaultActiveKey={defaultKey}>
<Accordion.Item eventKey={eventKey}>
<Accordion.Button as={Nav.Link} className="d-flex justify-content-between align-items-center">
<span>
<span className="sidebar-icon"><FontAwesomeIcon icon={icon} /> </span>
<span className="sidebar-text">{title}</span>
</span>
</Accordion.Button>
<Accordion.Body className="multi-level">
<Nav className="flex-column">
{children}
</Nav>
</Accordion.Body>
</Accordion.Item>
</Accordion>
);