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

时间:2020-01-08 16:33:36

标签: javascript reactjs react-router react-router-dom

如何使用React Router的ResultItem.js组件将值从Result.js组件传递到Link组件?

ResultItem.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

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

<Route path="/:id" component={Result} />

1 个答案:

答案 0 :(得分:0)

<Link to={{ 
       pathname: `/item/${item.id}`,
       state: {id: item.id}
}}> 

您稍后致电:结果中的{id.id}

行不通?