当map()函数作用于json文件时,React props不工作

c9qzyr3d  于 2023-03-04  发布在  React
关注(0)|答案(2)|浏览(116)

我正在尝试从一个json文件中呈现信息,我已经在页面上显示了。我已经使用了 Bootstrap 来格式化卡片,但是当我尝试不使用 Bootstrap 格式化时,它仍然不起作用。有什么想法吗?
我的json文件:

[
    {
        "id" : 1,
        "projectname": "Having fun",
        "description": "Having fun that is the project",
        "image":"../assets/images/havingfun.png"
    },
    {
        "id" : 2,
        "projectname": "Weather App",
        "description": "Weather app that is the project",
        "image":"../assets/images/weather.png" 
    }, 
    {
        "id" : 3,
        "projectname": "Daily Planner",
        "description": "Daily planner is the project",
        "image": "../assets/images/planner.png"
      
    }, 
    {
        "id" : 4,
        "projectname": "Code Quiz",
        "description": "Multiple Choice Quiz is the project",
        "image": "../assets/images/MCQ.png"
            
    }, 
    {
        "id" : 5,
        "projectname": "Password Generator",
        "description": "Password generator that is the project",
        "image": "../assets/images/passwordgenerator.png" 
      
    },
    {
        "id" : 6,
        "projectname": "Horiseon",
        "description": "Horiseon that is the project",
        "image": "../assets/images/horiseon.png" 
        
    }
]

要将json文件导入到的Projects.js:

import React from 'react'
import Card from 'react-bootstrap/Card';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';
import ProjectCards from '../jsonfiles/projects.json';

function ProjectCard(props) {
    <Card style={{ width: '18rem' }}>3
        <Card.Img variant="top" alt={props.projectname} src={props.image} />
        <Card.Body>
            <Card.Title>{props.projectname}</Card.Title>
            <Card.Text>{props.description}</Card.Text>
            <button className="deployedURL">Deployed Site{props.deployedURL}</button>
            <button className="githubURL">Github{props.githubUrl}</button>
        </Card.Body>
    </Card>
}

function Projects() {
    return (
        <div className="projectsAll">
            <div className="projectsTitle">
                <h1>Projects</h1>
                <p>Here are a few of the projects that have been create based on the client's requirements and design ideas</p>
            </div>
            <Row xs={1} md={2} className="g-4">
                {Array.from({ length: 6 }).map((_, idx) => (
                    <Col>
                        <Card>
                            <Card.Body>
                                <Row>
                                    <Col sm={6} className='softwareUsed'>
                                        <h1>HTML</h1>
                                        <p></p>
                                        <hr></hr>
                                        <h1>CSS</h1>
                                        <p></p>
                                        <hr></hr>
                                        <h1>JavaScript</h1>
                                        <p></p>
                                        <hr></hr>
                                    </Col>
                                    <Col sm={6} className='projectMiniCard'>
                                        {ProjectCards.map((project)=> (
                                            <ProjectCard
                                            projectname={project.projectname}
                                            image={project.image}
                                            description={project.description}
                                            />
                                        ))}
                                    </Col>
                                </Row>
                            </Card.Body>
                        </Card>
                    </Col>
                ))}
            </Row>
        </div >
    )
}

从我的app.js

import 'bootstrap/dist/css/bootstrap.min.css';
import Header from './pages/Header';
import {BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import Homepage from './pages/Homepage';
import Contact from './pages/Contact';
import About from './pages/AboutMe';
import Projects from './pages/Projects';

function App() {
  return (
    <Router>
         <Header /> 
         <Routes>
          <Route path="/" element={<Homepage />} />
          <Route path="/aboutme" element={<About />} />
          <Route path="/projects" element={<Projects />} />
          <Route path="/contactme" element={<Contact />} />
         </Routes>
    </Router>
    
  )
}

export default App;

想从json文件的信息显示在网页上。

1l5u6lss

1l5u6lss1#

问题是ProjectCard组件没有返回任何东西,它应该返回jsx。
代码应为:

function ProjectCard(props) {
    return (
        <Card style={{ width: '18rem' }}>3
            <Card.Img variant="top" alt={props.projectname} src={props.image} />
            <Card.Body>
                <Card.Title>{props.projectname}</Card.Title>
                <Card.Text>{props.description}</Card.Text>
                <button className="deployedURL">Deployed Site{props.deployedURL}</button>
                <button className="githubURL">Github{props.githubUrl}</button>
            </Card.Body>
        </Card>
    );
}
pgpifvop

pgpifvop2#

ProjectCard函数没有返回任何东西。另外,不要忘记在迭代时添加键prop。请查看<Card style={{ width: '18rem' }}>3行末尾的拼写错误

相关问题