【问题标题】:couldn't get to detail page for each restaurant from my Restaurant list无法从我的餐厅列表中访问每家餐厅的详细信息页面
【发布时间】:2021-08-04 14:55:46
【问题描述】:

我有一份从 Firestore 中检索到的餐厅列表,单击餐厅名称后,我必须返回每个餐厅的详细信息页面。 在 ProductGrid 页面上,我已返回已存储在 firestore 中的所有餐厅,然后从每个餐厅名称中我应该转到 ProductDetail,其中将包含每个餐厅的详细信息(动态路由) 我已经创建了路线,但没有返回任何页面我无法弄清楚到底是什么问题! 这是通往productDetail页面的链接

ProductGrid.js

      <h2> <Link to={`ecom-product-detail/${data.id}`}>{data.data.name_restaurant}</Link> </h2><<

ProductDetail.js

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

import { Link } from "react-router-dom";
import { firestore } from "../../../../../fire";

function ProductDetail(){

   const {productId}= useParams();
   const thisProduct = firestore.collection("Restaurants").doc().get.find((prod) => prod.id === productId);

return(

  
      <div className="col-12">
      <div className="card">
            <h1>{thisProduct.name_restaurant}</h1>
            <p> Price:${thisProduct.Currency}</p>
            <p> {thisProduct.email}          </p>
      </div>
      </div>
   
      
    ) ; }
    export default ProductDetail;

Route.js

    const Routes = () => {
    const routes = [
      { url: "ecom-product-detail/:productId", component: ProductDetail },
      ];
      return (
      <Fragment>
         <Switch>
            {routes.map((data, i) => (
               <Route
                  key={i}
                  exact
                  path={`/${data.url}`}
                  component={data.component}
               />
            ))}
         </Switch>

         {/* <Footer /> */}
      </Fragment>
   );
};

export default Routes;



这是我从 firestore 检索到的餐厅列表

这是我点击餐厅名称时呈现的页面

【问题讨论】:

    标签: javascript reactjs google-cloud-firestore react-router


    【解决方案1】:

    您应该在useEffect 挂钩内调用thisProduct,并将结果绑定到useState 挂钩。

    类似这样的:

    const {productId}= useParams();
    const [thisProduct, setThisProduct] = useState({});
    useEffect(() => {
    const productDetails = firestore.collection("Restaurants").doc().get.find((prod) => prod.id === productId);
       setThisProduct(productDetails);
    });
    

    【讨论】:

    • 感谢互动!但这并没有改变任何东西,即使我只想返回文本“hello world”,重新加载相同的页面也没有返回任何内容!我认为问题是来自 ProductGrid 的路由这是我写的链接我应该在这里改变什么吗? ` ecom-product-detail/${data.data.id}}>{data.data.name_restaurant}`
    • 如果你有 Github repo 或 Stack Blitz 项目,我可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多