[英]Element type is invalid React and React-Bootstrap
我正在使用 React 和 React-Bootstrap。
這是我創建組件的代碼:
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 eventKey="tables/" title="Tables" icon={faTable}>
<NavItem title="Bootstrap Table" link={Routes.BootstrapTables.path} />
</CollapsableNavItem>
這是我得到的錯誤:
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`.
為什么我會收到上述錯誤?
我改變了什么:我在導出默認值中有“CollapsableNavItem”方法,但現在我改變了它,現在我把它放在外面了,但是我仍然遇到同樣的錯誤。
如果你正在創建一個組件 < CollapsableNavItem />
您需要將以下內容移出export default
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>
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.