将链接添加到反应组件的onclik

问题描述 投票:-1回答:1

我想将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>
  );
}`
reactjs
1个回答
0
投票

使用history.pushhistory的对象由Route组件提供,就像在调用<Route render={props => <NewsItemBox {...props} />} />时一样,这些道具包含history,它本身具有有用的功能可以为您提供帮助。


0
投票

您为什么不使用history.push()

history中有一个props对象被Route传递了下来。

const { history } = props; 

<div className="col-lg-4 col-md-6" onClick={()=> history.push("/anotherpage")}>
© www.soinside.com 2019 - 2024. All rights reserved.