Card returns Undefine

huangapple go评论68阅读模式
英文:

Card returns Undefine

问题

I'm trying to create a reusable component by using useParams, however, I cannot pass the id since it always says undefined.

I'm not really sure what's wrong since I don't know why the values aren't being passed. This is my first time with routing, so any help is appreciated. Thanks a lot.

card.jsx

import React from 'react';
import { useNavigate, Link } from 'react-router-dom';

const Card = (props) => {
  console.log(props.title);

  return (
    <div>
      <h2>{props.id}</h2>
      <h2>{props.title}</h2>
      <p>{props.price}</p>
      <Link to={`/cards-details/${props.id}`}>
        <p>Buy Now</p>
      </Link>
    </div>
  );
}

export default Card;

cardDetails.jsx

import React from 'react';
import { useParams } from 'react-router-dom';
import cardData from './cardData';

function CardDetails(props) {
  const { id } = useParams();
  console.log(props.key);

  const selectedCard = cardData.find((card) => card.id === id);
  return (
    <div>
      <h1>{id}</h1>
    </div>
  );
}

export default CardDetails;

cardwrapper.jsx

import React, { useState } from 'react';
import Card from './Card';
import CardDetails from './CardDetails';

function CardWrapper({ cards }) {
  return (
    <div className='grid grid-cols-4 place-items-center'>
      {cards.map((card) => (
        <Card key={card.id} title={card.title} price={card.price} />
      ))}
    </div>
  );
}

export default CardWrapper;

app.jsx

function App() {
  const [open, setopen] = useState(true);

  return (
    <>
      <div className="">
        <Router>
          {open && (
            <>
              <Banner />
              <Navbar />
            </>
          )}
          <Routes>
            <Route path='/' element={<Home funcNav={setopen} />} />
            <Route path='/auth' element={<Auth funcNav={setopen} />} />
            <Route path='/cart' element={<Cart />} />
            <Route path='/sale' element={<All />} />
            <Route path='/about' element={<About />} />
            <Route path='/faq' element={<Faq />} />
            <Route path='/register' element={<Register />} />
            <Route path="/cards-details/:id" element={<CardDetails />} />
          </Routes>
        </Router>
      </div>
    </>
  );
}

export default App;
英文:

I'm trying to create a reusable component by using useparams however i cannot pass the id since it always say undefined

im not really sure whats wrong since idk why the values aren't being passed, this is my first time with routing so any help is appreciated thanks a lot

card.jsx

import React from &#39;react&#39;;
import { useNavigate, Link} from &#39;react-router-dom&#39;;



const Card = (props) =&gt; {

 console.log(props.title)


  return (
    &lt;div&gt;
      &lt;h2&gt;{props.id}&lt;/h2&gt;
      &lt;h2&gt;{props.title}&lt;/h2&gt;
      &lt;p&gt;{props.price}&lt;/p&gt;
      &lt;Link to={`/cards-details/${props.id}`}&gt;
              &lt;p&gt;Buy Now&lt;/p&gt;
            &lt;/Link&gt;
    &lt;/div&gt;
  );
}

export default Card;  

cardDetails.jsx

import React from &#39;react&#39;;
import { useParams } from &#39;react-router-dom&#39;;
import cardData from &#39;./cardData&#39;;


function CardDetails(props) {
  const {id} = useParams()
  console.log(props.key)
  
  const selectedCard = cardData.find(card =&gt; card.id === id);
  return (
    &lt;div&gt;
        
      &lt;h1&gt;{id}&lt;/h1&gt;
      
    &lt;/div&gt;
  );
}

export default CardDetails;

cardwrapper.jsx

import React, { useState } from &#39;react&#39;;
import Card from &#39;./Card&#39;;
import CardDetails from &#39;./CardDetails&#39;;

function CardWrapper({ cards }) {
 

  return (
    &lt;div className=&#39;grid grid-cols-4 place-items-center&#39;&gt;
      {cards.map(card=&gt; (
        &lt;Card key={card.id} title={card.title} price={card.price} /&gt;
      ))}
    &lt;/div&gt;
  );
}

export default CardWrapper;

app.jsx

function App() {
  const [open, setopen] = useState(true)

  return (
    &lt;&gt;
   
    &lt;div className=&quot;&quot;&gt;
    
     &lt;Router&gt;
      { open &amp;&amp;
       &lt;&gt;
       &lt;Banner /&gt;
          &lt;Navbar /&gt;  
       &lt;/&gt;
      }
        &lt;Routes&gt;
          &lt;Route path=&#39;/&#39; element={&lt;Home funcNav={setopen}/&gt;}/&gt;
          &lt;Route path=&#39;/auth&#39; element={&lt;Auth funcNav={setopen}/&gt;}/&gt;
          &lt;Route path=&#39;/cart&#39; element={&lt;Cart /&gt;}/&gt;
          &lt;Route path=&#39;/sale&#39; element={&lt;All /&gt;}/&gt;
          &lt;Route path=&#39;/about&#39; element={&lt;About /&gt;}/&gt;
          &lt;Route path=&#39;/faq&#39; element={&lt;Faq /&gt;}/&gt;
          &lt;Route path =&#39;/register&#39; element={&lt;Register /&gt;}/&gt;
          &lt;Route path = &quot;/cards-details/:id&quot; element={&lt;CardDetails/&gt;}/&gt;
             
          
  
          
        &lt;/Routes&gt;
      &lt;/Router&gt;
    &lt;/div&gt;
    &lt;/&gt;
  )
}

export default App

答案1

得分: 0

将这个代码从原文改为以下内容:

 &lt;Card 
  key={card.id} 
  id={card.id} 
  title={card.title} 
  price={card.price} /&gt;
英文:

Change this:

&lt;Card 
  key={card.id} 
  title={card.title} 
  price={card.price} /&gt;

to this (it's missing the id):

 &lt;Card 
  key={card.id} 
  id={card.id} 
  title={card.title} 
  price={card.price} /&gt;

huangapple
  • 本文由 发表于 2023年4月4日 11:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定