简体   繁体   English

如何动态显示消息 react-router-dom

[英]how to dynamically show messages react-router-dom

currently working on an api to display set of movies...i want it working so that when i click on a title it redirects me to a different page with the name of the title as the heading..i have already set state on my code to do that but when i click on the link or title it redirects successfully but doesn't show the title it's as if the state got destroyed on redirecting giving me an error of "Title undefined" when i redirect.. This is my route for handling the link目前正在使用 api 来显示电影集...我希望它可以正常工作,这样当我单击标题时,它会将我重定向到以标题名称作为标题的不同页面..我已经在我的执行此操作的代码,但是当我单击链接或标题时,它成功重定向但不显示标题,就好像 state 在重定向时被破坏,当我重定向时出现“标题未定义”错误。这是我的路线用于处理链接

import React,{useState} from "react"
import {Link,Redirect} from "react-router-dom"
import Detail from "./Details"
  function Latest(props){
     const [Name,SetName] = useState(null)
       function Click(title){
       SetName(title)
       }
    return (
     <div>
      <div className = "row">
       {props.popular.map(function(popular){
  return (
  <div className = "col-lg-3 col-md-4 col-sm-6" style = {{marginBottom:"2rem"}} onClick = {() => props.id(popular.id)}>
  <div className="card">
  <img src= {"https://image.tmdb.org/t/p/original" + popular.backdrop_path} className="card-img-top" alt={popular.title} />
  <div className="card-body">
    <h3 className = "card-title">{popular.title}</h3>
    <p className = "card-text">{popular.overview}</p>
    <Link to = "/Title"><button type = "button" onClick = {() => Click(popular.title)}>Submit</button></Link>
    <Detail detail = {Name}/>
  </div>
 </div>
 </div>
   )
     })}
    </div>
      </div>
    )
     }

    export default Latest

App.js file应用程序.js 文件

import {BrowserRouter,Route} from "react-router-dom"
 import Home from "./Home"
 import Detail from "./Details"
   function App(popular) {
     return (
      <div>
     <BrowserRouter>
      <div>
    <Route path = "/" exact component = {Home}/>
     <Route path = "/Title" component = {Detail}/>
     </div>
     </BrowserRouter>
     </div>
       )
         }

       export default App;

and my Details file和我的详细信息文件

      import Latest from "./LatestMovies"
           function Detail(props){
             return <h1>{props.detail}</h1>
  }

  export default Detail

It wont show you the title anymore because see you are trying to display title in a different file(where you are having your link to title and other stuff) and as soon as you are directed to Title page,now the content of Title page would be shown to you.Not the Content of the page that you are currently in(which has your Details.js file)它不会再向您显示标题,因为看到您正在尝试在不同的文件中显示标题(您在其中有标题和其他内容的链接)并且一旦您被定向到标题页面,现在标题页面的内容就会显示给您。不是您当前所在页面的内容(其中包含您的 Details.js 文件)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM