Reactjs 尝试将变量值放入URL值的链接时出现问题
尝试在我的链接组件中构造URL时出现此错误:Reactjs 尝试将变量值放入URL值的链接时出现问题,reactjs,Reactjs,尝试在我的链接组件中构造URL时出现此错误: _renderSubNav() { const { dispatch } = this.props let userId = this.props.match.params.userId; console.log('userId is' + userId); return ( <div className=""> <nav aria-label="breadcrumb">
_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>
);
}
bundle.js:2567警告:React.createElement:类型无效--
应为字符串(用于内置组件)或类/函数(用于
复合组件)但得到:未定义。你可能忘了出口
您的组件来自定义它的文件,或者您可能已经混合了
启动默认导入和命名导入
以下是我的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>
);
}
\u renderSubNav(){
const{dispatch}=this.props
让userId=this.props.match.params.userId;
log('userId为'+userId');
返回(
用户
);
}
控制台会在chrome中记录id,因此用户id不是未定义的,例如2。
如何将用户ID放入链接组件中?这听起来像是与链接不同的问题 你在某处丢失了一个返回声明。从我所记得的关于链接的一切来看,您的语法很好,抛出的错误不是引用链接 您需要使用
import {Link} from 'react-router-dom';
重要提示:
{Link}
而不是Link
。这是您的代码沙盒中的错误,很可能也是您的实际代码中的错误。问题在于您没有向我们展示的代码。请显示调用此函数的代码method@DanielHilgarth如果我删除了/users
渲染的组件,显示其代码too@Blankman:我认为您需要创建一个最小的可复制示例,因为您显示的代码很好。是否导入了链接
组件?