简体   繁体   English

如何使用“react-router-dom”中的“链接”使“div”标签可点击?

[英]How to make "div" tag clickable with "Link" from "react-router-dom"?

I have the following code:我有以下代码:

 import React, { Component } from 'react'; import '../styles/CountryDetails.css'; import { Link } from 'react-router-dom'; import { IconContext } from 'react-icons'; import { GoArrowRight } from 'react-icons/go'; import { FaPlane } from 'react-icons/fa'; class CountryDetails extends Component { constructor(props) { super(props); this.state = { countryData: props.countryData } } render() { console.log(this.state.countryData); return ( <div> <h1 className="display-3 text-center my-5">{this.state.countryData.countryClassification}</h1> <div className="CountryDetail"> <div className="cards shadow-1 container"> <div className="row vertical-align"> <div className="col-2 text-center"> <IconContext.Provider value={{ color: '#A9A9A9', className: 'icon-hover icon-size'}}> <div> <FaPlane /> </div> </IconContext.Provider> </div> <div className="col-8"> <h4>Airfare | Car Rental | Hotel Booking Site</h4> {this.state.countryData.topAirfareCarRentalHotelBookingSite1? null: <span className="category-status">Under Construction...</span>} </div> <div className="col-2 text-center"> <IconContext.Provider value={{ className: 'show-hover icon-arrow-size'}}> <div> <GoArrowRight /> </div> </IconContext.Provider> </div> </div> <Link to={`/country/${this.state.countryData.countryAndTerriority}/topAirfareCarRentalHotelBookingSite1`} /> </div> </div> </div> ) } } export default CountryDetails;

This is what I get:这就是我得到的:

在此处输入图像描述

Right now, my URL link is showing http://localhost:8080/country/Afghanistan/ .现在,我的 URL 链接显示http://localhost:8080/country/Afghanistan/ When I click on the rectangle box, I expect my URL link to update to http://localhost:8080/country/Afghanistan/topAirfareCarRentalHotelBookingSite1 and it will show up a different component.当我单击矩形框时,我希望我的 URL 链接更新为http://localhost:8080/country/Afghanistan/topAirfareCarRentalHotelBookingSite1并且它会显示一个不同的组件。

For some reason, I cannot make this div clickable and the URL does not change when I click on it.出于某种原因,我无法使此 div 可点击,并且当我点击它时 URL 不会改变。 Am I using this code我在使用这段代码吗

<Link to={`/country/${this.state.countryData.countryAndTerriority}/topAirfareCarRentalHotelBookingSite1`} />

wrong or putting it in the wrong place?错了还是放错地方了?

have you tried to put the div inside the link?您是否尝试将 div 放在链接中?

<Link to = "yourURL" >
   <div></div>
</Link>

Your code syntax is wrong.您的代码语法错误。 Wrap the div tag with the Link tag用 Link 标签包裹 div 标签

<Link to ='...'><div>....</div></Link>

As an alternative to the other answers, you can use a functional component and do something like this:作为其他答案的替代方案,您可以使用功能组件并执行以下操作:

import React, { useCallback } from 'react'
import { useHistory } from 'react-router-dom'

export default ({ countryData }) => {

    const history = useHistory()
    const onClick = useCallback(() => {
        const to = `/country/${countryData?.countryAndTerriority}/topAirfareCarRentalHotelBookingSite1`
        history.push(to)
    },[countryData, history])

    return (
        <div onClick={onClick}>
            {'Your content here'}
        </div>
    )

}

EDIT:编辑:

I think this is the best approach in your case, you don't really need a state for this component, because you already have the props.我认为这是您情况的最佳方法,您实际上并不需要此组件的 state,因为您已经有了道具。

I used this part of code:我使用了这部分代码:

<Link to = "yourURL" >
<div></div>
</Link>

It looks like Link is disableing that div styles. (As user Beast mentioned right above here in comments.) In my case div was empty only with bg so setting up div height: 100% solved the problem.看起来 Link 正在禁用该 div styles。(正如用户 Beast 在评论中上面提到的那样。)在我的例子中,div 是空的,只有 bg 所以设置 div height: 100% 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM