Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - React Router not displaying content from json file

Hello again Stackoverflow members.I have GLS I have more similiar components to GLS concept is the same but styling is not, anyway the thing I want to achieve is this, When I press on the Links which are located in HomeComponent/MainPage I want to display/Render the image class and price of that Component in this case A/GLS Components for some reason it does not want to show the data from json file 'I might be blind'. I would be very grateful if someone could help me.

Gls Component
import React from "react";
import data from "../data.json";
import { Link } from "react-router-dom";
function GLS({product}) {
  return (
    <div>
          <div key={product.id} className="m-4 bg-blue-100 rounded-xl p-8 absolute ">
            <div>
              <Link
                to={`/payment/${product.id}`}
                className="py-1 px-2 text-black-600 h-10  ml-24 mt-32 bg-white w- 
            36 rounded-full focus:outline-none focus:ring-2 focus:ring-gray-600"
              >
                Buy Now
              </Link>
              <img
                alt=""
                className="w-screen object-contain"
                src={product.image}
              ></img>
              <h1 className=" ml-24 md:text-5xl sm:text-5xl  top-8">
                {product.class}
              </h1>
              <h1 className="text-base font-mono ml-24 top-24">
                {product.price}
              </h1>
            </div>
          </div>
    </div>
  );
}

export default GLS;
App Component
import React,{useState, useEffect} from 'react'
import './assets/main.css'
import A from './Models/GLS Model/A'
import GLS from './Models/GLS Model/GLS'
import data from "./Models/data.json";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";
return (
    <div className='' >       
     <div >
       <Router>           
        <Switch>
          <Route  path="gls">
          {data.map((product) => (
              <GLS key={product.id} product={product} />
            ))}
            </Route>
          <Route  path="a">
          {data.map((product) => (
              <A key={product.id} product={product} />
            ))}
         </Route> 
            <Route path="/payment/:productId">
            <Payment />
          </Route>
          <Route exact path="/">
            <Home />
          </Route>
        </Switch>  
       </Router>
     </div>
    </div> 
  );
}

export default App;

import React from 'react'
import {
    Link, 
  } from "react-router-dom";
import data from "./Models/data.json";

function Home() {
    return (
        <div className='ml-20'>
          <nav className='bg-red-50 max-w-full'>
            <ul >    
            <li>           
           <Link to='GLS'>GLS-class</Link>
            </li>           
            <li>
             <Link to="/A"> A-Class</Link>
            </li> 
          </ul>
        </nav>
        </div>
    )
}
export default Home


Payment Component

import React from "react";
import { useParams } from "react-router-dom";
import data from "./Models/data.json";

const Payment = () => {
  const { productId } = useParams();
  const filteredData = data.filter((product) => product.id === productId)[0];

  return (
    <div className="ml-20">
      <img alt="" className="w:2/4 object-contain " src={filteredData.image} />
      <h2
        className=" ml-24 mlg:ml-6 mlg:mt-2 mlg:static text-5xl mlg:text-2xl text-blue-400 absolute top- 
        48"
      >
        {filteredData.class}
      </h2>
      <h3
        className="text-lg  mlg:mb-2 mlg:ml-6  mlg:mt-2 mlg:static font-bold text-green-800 
                 font-mono ml-24 top-64  absolute"
      >
        {filteredData.price}
      </h3>
    </div>  
  );
};

export default Payment;

Json file
[
{
"id": 0,
"class": "A-Class",
"image": "./ModImages/Aclass.jpg",
"price": "It goes around $37,300",
},

{
"id": 1,
"class": "GLS-Class",
"image": "./ModImages/GLSclass.jpg",
"price": "It goes around $47,700"

}
]


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

1 Answer

0 votes
by (71.8m points)

change

<Route path="gls">

to

<Route path="/gls">

in App Component

and change

<Link to='GLS'>GLS-class</Link>

to

<Link to='/gls'>GLS-class</Link>

in Home Component and change

{
"id": 0,
"class": "A-Class",
"image": "./ModImages/Aclass.jpg",
"price": "It goes around $37,300",
},

{
"id": 1,
"class": "GLS-Class",
"image": "./ModImages/GLSclass.jpg",
"price": "It goes around $47,700"
}
]

to

[
 {
  "id": 0,
  "class": "A-Class",
  "image": "./ModImages/Aclass.jpg",
  "price": "It goes around $37,300"
 },
 {
  "id": 1,
  "class": "GLS-Class",
  "image": "./ModImages/GLSclass.jpg",
  "price": "It goes around $47,700"
 }
]

in data json file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...