我在尝试在链接组件中构建URL时收到此错误:
bundle.js:2567警告:React.createElement:type无效 - 期望一个字符串(对于内置组件)或类/函数(对于复合组件)但得到:undefined。您可能忘记从其定义的文件中导出组件,或者您可能混淆了默认导入和命名导入。
这是我的React组件中的内容:
_renderSubNav() {
const { dispatch } = this.props
let userId = this.props.match.params.userId;
console.log('userId is' + userId);
return (
<div className="">
<nav aria-label="breadcrumb">
<ol className="breadcrumb">
<li className="breadcrumb-item"><Link to={`/users/${userId}`} className="nav-link">User</Link></li>
</ol>
</nav>
</div>
);
}
控制台会在chrome中记录id,因此userId不是未定义的,例如2.如何将userId放入Link组件?
你需要使用
import {Link} from 'react-router-dom';
重要:{Link}
而不是Link
。这是您的代码框中的错误,很可能也在您的实际代码中。
听起来有一个与Link不同的问题。
你错过了一个返回声明。从我记得的所有关于Link的内容来看,你的语法很好,抛出的错误并没有引用Link。