简体   繁体   English

react-slick carousel 以某种方式添加到每个图像的链接

[英]react-slick carousel somehow adding link to every image

I am working on a Gatsby project and am having issues with a homepagebanner.我正在从事 Gatsby 项目,但遇到了主页横幅问题。 Using react-slick which works ok, but adding content on top of each pictures is causing problems.使用可以正常工作的 react-slick,但是在每张图片的顶部添加内容会导致问题。 Setting a link for the second picure ("golflink") is resulting in every image now being clickable leading to this href.为第二张图片(“golflink”)设置链接会导致现在每张图片都可以点击并指向此 href。 Can someone help rewrite so only the second image is leading to the event page?有人可以帮助重写,所以只有第二张图片会导致活动页面吗?

link to index page with banner below navbar https://zen-aryabhata-bd8020.netlify.app/链接到导航栏下方带有横幅的索引页面https://zen-aryabhata-bd8020.netlify.app/

Thank you in advance.先感谢您。

import "slick-carousel/slick/slick.css"; 
import "slick-carousel/slick/slick-theme.css";
import Slider from "react-slick"
import { Link } from "gatsby";

import "../styles/bannerstyles.css"

import banner_image_1 from '../img/banner1.jpg'
import banner_image_2 from '../img/golferin.jpg'
import banner_image_3 from '../img/baby.jpg'
import golf_cup_logo from '../img/golf_cup_logo.png'


const photos = [
    {
    id: '1',
    url: banner_image_1,
    css_id: "banner-image-one",
    heading: 'unsere mission',
    description: 'Lebensrettende Hilfe für die Schwächsten',
    link: "/projekte",
    link_text: "mehr erfahren",
    },
    {
    id: '2',
    url: banner_image_2,
    css_id: "banner-image-two",
//  description: 'Dabei sein und (Golf-) spielend Gutes tun!',
    link_text: "mehr erfahren",
    link: "/events#golf-turnier",
    badge: golf_cup_logo,
    golflink: "/events#golf-turnier"
    },
    {
    id: '3',
    url: banner_image_3,
    css_id: "banner-image-three",
    heading: 'unsere projekte',
    description: 'Hilfe, die dort ankommt, wo sie am dringensten gebraucht wird!',
    link: "/projekte",
    link_text: "mehr erfahren"
    }
]


class HomepageBanner extends Component{
    render(){
    const settings = {
        dots: true,
        fade: true,
        infinite: true,
        slidesToShow: 1,
        slidesToScroll: 1,
        autoplay: true,
        autoplaySpeed: 5000,
    }
    return (
        <div id="homepagebanner">
        <Slider {...settings}>
            {photos.map((photo) => {
            if (photo.id = 1) {
            return (
            <a href="/events#golf-turnier">
                <div id="homepagebanner-img-container">
                <img id="homepagebanner-img" width="100%" alt="homepagebanner-image" src={photo.url} />
                <div className="homepagebanner-text" id={photo.css_id}>
                    <div>
                <img src={photo.badge} width="100%" position="absolute" className="banner-badge"/>
            </div>
                <h1>{photo.heading}</h1>
                <p>{photo.description}</p>
                <a className="banner-link" href={photo.link}>{photo.link_text}</a>
                </div>
                </div>
                </a>
            )
            } else {
            return(
                <div id="homepagebanner-img-container">
                <img id="homepagebanner-img" width="100%" alt="homepagebanner-image" src={photo.url} />
                <div className="homepagebanner-text" id={photo.css_id}>
                <h1>{photo.heading}</h1>
                <p>{photo.description}</p>
                <a className="banner-link" href={photo.link}>{photo.link_text}</a>
                </div>
                </div>
            )
            }
        })}
        </Slider>
        </div>
    )
    }
}

export default HomepageBanner

You have hardcoded the link value <a href="/events#golf-turnier"> thats wrapped around everything including a link.您已经硬编码了链接值<a href="/events#golf-turnier"> ,它包含在包括链接在内的所有内容中。

Also your if statement should be if (photo.id == 1) {你的 if 语句也应该是 if (photo.id == 1) {

You are assigning a value in a condition instead of comparing values:您在条件中分配一个值,而不是比较值:

(photo.id = 1)

Should be:应该:

(photo.id === 1)

Even == will work but for coersion better use ===即使==也可以,但为了更好地使用强制===

In addition, you are missing the key attribute in each rendered element.此外,您在每个渲染元素中都缺少key属性。 The key helps React identify which items have changed, are added, or are removed and it should be a unique value, so the photo.id fits perfectly its definition.key有助于 React 识别哪些项目已更改、添加或删除,并且它应该是一个唯一值,因此photo.id完全符合其定义。

Your final code should look like:您的最终代码应如下所示:

return (
  <div id="homepagebanner">
    <Slider {...settings}>
      {photos.map((photo) => {
        if (photo.id === 1) {
          return (
            <a href="/events#golf-turnier" key={photo.id}>
              <div id="homepagebanner-img-container">
                <img id="homepagebanner-img" width="100%" alt="homepagebanner-image" src={photo.url} />
                <div className="homepagebanner-text" id={photo.css_id}>
                  <div>
                    <img src={photo.badge} width="100%" position="absolute" className="banner-badge" />
                  </div>
                  <h1>{photo.heading}</h1>
                  <p>{photo.description}</p>
                  <a className="banner-link" href={photo.link}>{photo.link_text}</a>
                </div>
              </div>
            </a>
          );
        } 
        return (
          <div id="homepagebanner-img-container" key={photo.id}>
            <img id="homepagebanner-img" width="100%" alt="homepagebanner-image" src={photo.url} />
            <div className="homepagebanner-text" id={photo.css_id}>
              <h1>{photo.heading}</h1>
              <p>{photo.description}</p>
              <a className="banner-link" href={photo.link}>{photo.link_text}</a>
            </div>
          </div>
        );
        
      })}
    </Slider>
  </div>
);

More details about key : https://reactjs.org/docs/lists-and-keys.html有关key的更多详细信息: https://reactjs.org/docs/lists-and-keys.html

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

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