简体   繁体   English

如何使用 React 将点击的卡片从上部组件 CardList 显示到 slider 下来?

[英]How can I show the clicked card from the upper component CardList to the slider down with React?

该部分中的组件图片

I am using React Slick for the slider.我正在为 slider 使用React Slick I have a Card component for the cards and also I have Slider section where I render the CardList component for the upper card with mapping data from data.json and SlickSlider component for the slider.我有卡片的 Card 组件,还有 Slider 部分,其中我使用来自 data.json 的映射数据和 slider.slider.

I know that I have to add onClick event on the Card component but that's it.我知道我必须在 Card 组件上添加 onClick 事件,仅此而已。

const NewSliderSection = () => {

  return (
    <div className='new-slider-section'>
      <CardList />
      <SlickSlider />
    </div>
  )
}

export default NewSliderSection
import React from 'react'
import "./CardList.css"
import { useState, useEffect } from "react";
import Card from '../Card/Card';


const CardList = () => {
  const [cardData, setCardData] = useState([]);

  const getData=()=>{
    fetch('data.json'
    ,{
      headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json'
       }
    }
    )
      .then(function(response){
        console.log(response)
        return response.json();
      })
      .then(function(myJson) {
        console.log(myJson);
        setCardData(myJson)
      });   
  }
  useEffect(()=>{
    getData()
  },[])

  return (
    <div className='card-list-container'>
      {cardData.length > 0 && cardData.map((card) => (
        <Card key={card.id} title={card.title} src={card.src}/>
      ))}
      {cardData.length === 0 && <h3>Loading...</h3>}
    </div>
  )
}

export default CardList
import React, { useEffect, useState } from "react";
import Slider from "react-slick";
// import CardList from "../CardList/CardList";
import "slick-carousel/slick/slick.css"; 
import "slick-carousel/slick/slick-theme.css";
import Card from "../Card/Card";
import "./SlickSlider.css"

const SlickSlider = () => {
  const [cardData, setCardData] = useState([]);

  const settings = {
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: true,
  };


  const getData=()=>{
    fetch('data.json'
    ,{
      headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json'
       }
    }
    )
      .then(function(response){
        console.log(response)
        return response.json();
      })
      .then(function(myJson) {
        console.log(myJson);
        setCardData(myJson)
      });   
  }
  useEffect(()=>{
    getData()
    console.log(cardData)
  },[])

  return (
    <div className="slick-slider">
      <Slider {...settings}>
        {cardData.map((card) => (
          <Card src={card.src} title={card.title} key={card.id} />
        ))}
        {/* <div>HELLO</div> */}
      </Slider> 
    </div>
  )
}

export default SlickSlider

The code below may work:下面的代码可能有效:

store reference of the slider slider 的存储参考

const sliderRef = useRef(null);

<Slider ref={sliderRef} ...>
...
</Slider>

and add click listener on the card component with slickGoTo method ( https://react-slick.neostack.com/docs/api )并使用 slickGoTo 方法在卡片组件上添加点击监听器( https://react-slick.neostack.com/docs/api

onClick={()=>{sliderRef.current.slickGoTo(index)}}

You can try to add simple button to see if it works您可以尝试添加简单的按钮,看看它是否有效

<button onClick={()=>{sliderRef.current.slickGoTo(3)}}>test</button>

You may know how to "forwardRef" to pass the reference of "SlickSlider" to "CardList ".您可能知道如何“forwardRef”将“SlickSlider”的引用传递给“CardList”。

暂无
暂无

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

相关问题 在 React js 中单击项目后如何显示组件? - How can I show a component after item clicked in react js? (react,antd,card) 如何使用 antd 卡组件阻止 onclick function 操作按钮? - (react,antd,card) How can I block the onclick function from action buttons with antd card component? 为什么我不能在 JSX 中导入这个 CardList? - Why can't I import this CardList in JSX? 如何将样式从反应语义 UI 卡组件传递到 img 标签 - How to pass down style into img tag from react semantic-ui Card component 当我单击其中一项(输入元素)时,如何阻止 Select 组件关闭 - 反应 antd - How can I stop the Select component from closing when I clicked one of its item(Input element) - react antd 如何在 React Native 中有条件地隐藏和显示卡片按钮组件 - How to conditionally hide and show a card button component in React Native React admin:如何在创建组件中显示一些数据,从单击创建按钮的页面 - React admin: how to show some data in create component, from the page where create button was clicked 如何在 React.Component 中显示/隐藏表单 div? - How can I show/hide a form div in React.Component? 如何从隐藏和重新渲染中显示我的react组件? - How can I show my react component from hide and re-render again? 如何在同一个组件中使用 react useContext 来显示上下文中的数据 - How can I use react useContext , to show data from context, in the same component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM