简体   繁体   English

如何通过 React Router Link 将值从功能组件传递到另一个功能组件?

[英]How can I pass values from a functional component through a React Router Link to another functional component?

How can I pass values from my ResultItem.js component, to my Result.js component using React Router's Link component?如何使用 React Router 的Link组件将值从我的ResultItem.js组件传递到我的Result.js组件?

ResultItem.js结果项.js

export default function ResultItem({ result }) {
//desctructure the result elements
const {
    title,
    type,
    id,
    publisher,
    volume,
    issue,
    page_number,
    year_published,
} = result;

return (
    <>
        <div className="card grey lighten-4" style={{ padding: '1rem' }}>
            <span className="badge green white-text">{type}</span>
            <h5>{title}</h5>
            <h6>Publisher: {publisher}</h6>
            <h6>Volume: {volume}</h6>
            <h6>Issue: {issue}</h6>
            <h6>Page Number: {page_number}</h6>
            <h6>Year Published: {year_published}</h6>

            <div>
                <Link
                    to={`/${id}`}
                    className="waves-effect waves-light btn-small blue"
                >
                    Read More
                </Link>
            </div>
        </div>
    </>
);

}

Result.js结果.js

export default function Result({ title }) {
return (
    <>
        <div className="container">
            <h5>
                More information about that thing you just clicked on will be on this
                page.
            </h5>
            <h1>title = {title}</h1>
        </div>
    </>
);

}

App.js应用程序.js

<Route path="/:id" component={Result} />
<Link to={{ 
       pathname: `/item/${item.id}`,
       state: {id: item.id}
}}> 

Later you call: {id.id} in Result后来你在结果中调用: {id.id}

Does it works?它有效吗?

暂无
暂无

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

相关问题 React:为什么我不能将数据从一个功能组件传递到另一个功能组件? - React: Why can't I pass data from one functional component to another functional component? 如何将道具/状态从 React Link 传递到功能组件? - How can I pass props/state from a React Link to a functional component? 如何在 React 中将数组作为结果从一个功能组件传递到另一个功能组件 - How to pass array as result from one functional component to another in React 如何从一个反应功能组件调用功能方法到另一个反应功能组件? - How to call a functional method from one react functional component to another react functional component? ReactJS:如何在另一个功能组件中读取链接 react-router 中传递的值 - ReactJS : How to read value passed in Link react-router in another functional component Reactjs:将功能组件传递给另一个功能组件以 - Reactjs: Pass functional component to another functional component to 如何通过 React Navigation 将导航道具传入功能组件屏幕? - How to pass in navigation props into a functional component screen through React Navigation? 保存从 promise 返回的数据并将其传递给反应中的另一个组件 - Save and pass data returned from a promise to functional another component in react 在 React 中,我可以在另一个功能组件的主体中定义一个功能组件吗? - In React, can I define a functional component within the body of another functional component? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM