我想将onClick事件添加到我的组件中,从而导致单击,通过使用链接到路由器元素将其引导到新页面我已经尝试过以下代码:
import React from "react";
import { Link } from "react-router-dom";
export default function NewsItemBox(props: NewsItemBoxOptions) {
return (
<div className="col-lg-4 col-md-6" onClick={()=>{<Link to="./"></Link>}}>
<div className="single-blog-post">
hello world
</div>
</div>
);
}`
使用history.push
,history
的对象由Route
组件提供,就像在调用<Route render={props => <NewsItemBox {...props} />} />
时一样,这些道具包含history
,它本身具有有用的功能可以为您提供帮助。
您为什么不使用history.push()
?
history
中有一个props
对象被Route
传递了下来。
const { history } = props;
<div className="col-lg-4 col-md-6" onClick={()=> history.push("/anotherpage")}>