【发布时间】:2019-12-12 07:54:50
【问题描述】:
我有汉堡的菜单)有不同的项目,结果我想从不同的标题打开不同的组件)
const MenuItems = props => {
const { title, price, composition } = props;
let myComponent = require(`../` + `img/` + `${title}` + `.png`);
const onClick=()=>{
if(title!=='burger'){
return (<Item_burger />)
console.log(title)
}
}
return (
<div className="col-sm-6">
<img className="card-img-top mb-5" src={myComponent} alt="...."/>
<div className="card shadow-sm">
<div className="card-body text-center">
<h3>
<strong>{title}</strong>
</h3>
<p className="card-text">
<strong>{composition}</strong>
</p>
<h4>{price}</h4>
<Link to={`burger/constructor/${title}`}>
<button type="submit" className="btn btn-outline-light" onClick={onClick}>Go to Lab of taste =></button>
</Link>
</div>
</div>
</div>
)
}
【问题讨论】:
-
事件处理程序中的返回无处可返回。您正在寻找的预期行为是什么?
<Link>内带有 onClick 的按钮也没有意义 -
您可能希望
<Item_burger />在与链接路径关联的路由中呈现。建议大家学习一些路由器教程和文档中的例子 -
也发布您的路线。
-
你的
onClick没有用,因为你已经用Link包裹了你的button。一旦点击它就不会等待执行您的onClick函数并直接重定向到burger/constructor/${title}路径。你应该有 Route 来处理这个路径。
标签: reactjs react-router