简体   繁体   English

单击 React.js 将数据传递给另一个组件

[英]Passing data to another component on click React.js

I am trying to pass a specific id of the card that I am clicking to another component to show the card it self in another page我正在尝试将我单击的卡片的特定 ID 传递给另一个组件,以在另一个页面中显示它自己的卡片

import useFetchData from "./useFetchData";
import { Card,Row,Col, Container,CardGroup,Button } from 'react-bootstrap';
import { Link } from "react-router-dom/cjs/react-router-dom.min"
import Lyrics from "./LyricsFinder";


const Tracks = () => {

const {data,Load}=useFetchData();

return ( 
    <Container>
                {Load && <div>Loading...</div>} 
        {!Load && <h2 style={{ textAlign:"center",marginBottom:"25px" }} >Top 5 songs on the Bilboard this weak</h2>}
    <div className="tracks">
        <CardGroup>
        {data.map(info =>(
                    <div key={info.track.track_id}>
           <Card style={{ width: '30rem' }} key={info.track.track_id}>
            <Card.Body>
            <Card.Title>{info.track.track_name}</Card.Title>
            <Card.Text>  {info.track.artist_name} </Card.Text>
            </Card.Body>
            <Card.Footer variant="mx-auto ">
                 <Link to="/lyrics" className="btn btn-primary">Sign up</Link> // here where I want to click
       
            </Card.Footer>
            
            </Card>
        </div>
         ))
        }</CardGroup>
    </div>
    </Container>
     );
     }

  export default Tracks;

I want to pass an id of the card to the component where I am going to when I click on当我点击时,我想将卡片的 id 传递给我要去的组件

Would you please tell me how to do that?!你能告诉我怎么做吗?!

You can pass id as parameter,您可以将 id 作为参数传递,

<Link to={'/lyrics/' + id} className="btn btn-primary">Sign up</Link>

Now to receive on lyrics component, import useParams from react-router-dom现在接收歌词组件,从react-router-dom导入useParams

import { useParams } from "react-router-dom";

Now,现在,

const { id } = useParams();

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

相关问题 将数据 onClick 从子组件传递到 React.JS 中的另一个子组件 - Passing data onClick from child component to another child component in React.JS 将 React.js 中的 function 从一个 function 组件传递到另一个组件 - Passing function in React.js from one function component to another React.js 将数据从类组件传递到 ReactDOM.render 中的另一个组件 - React.js passing data from class component to another in ReactDOM.render 将数据传递到另一个组件后,为什么我不能在 React.js 中映射对象数组? - Why can't I map over an array of object in React.js after passing data to another component? 在 React.js 中调用 API 数据并将其传递给其他组件 - Calling and passing API data to other component in React.js 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS 在单击事件的反应函数组件中定义的调用函数形成另一个函数组件 - React.js - Calling function defined within a react function component on a click event form another function component - React.js React.js:如何在点击时附加组件? - React.js: How to append a component on click? React.js:将一个组件包装到另一个组件中 - React.js: Wrapping one component into another React.js插入另一个组件的ref - React.js insert into ref of another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM