Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

reactjs - How do I pass Page title to the menu (like breadcrumbs)

I am trying to develop a mobile view for an application. I have menus on my footer component, I have header component, pages and subpages (component).

  import React from 'react' 
    import {Link} from Gatsby
    export default Nav =() => {
         const menu = [
    {
       url: '/',
       title: 'Home', 
     },
     {
      url: '/about'
      title: 'About'
     },
      {
      url: '/contact'
      title: 'Contact'
     },
      
    ]
    return (
           <ul>
{menu.map(item => {
     <li>
<Link to="{item.title}">{item.title}</Link>
</li>
})}
</ul>
)
    }

For my header I have a place a component to display the content but I want to pass the clicked menu title on the header menu (another component entirely). I want to know how to go about it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use state props of <Link />

Here is an example:

const PhotoFeedItem = ({ id }) => (
  <div>
    {/* (skip the feed item markup for brevity) */}
    <Link
      to={`/photos/${id}`}
      state={{ fromFeed: true }}
    >
      View Photo
    </Link>
  </div>
)

const Photo = ({ location, photoId }) => {
  if (location.state.fromFeed) {
    return <FromFeedPhoto id={photoId} />
  } else {
    return <Photo id={photoId} />
  }
}

Or you can do it using the navigate function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...