NextJS:Props对象未定义

发布于 2025-02-09 07:57:38 字数 2213 浏览 0 评论 0原文

目标:映射存储在本地JSON文件中的“投资组合”项目,然后将每个对象传递给我创建的卡组件。

投资组合组件位于页面文件夹中,console.log(行:15-正确显示JSON对象的数组// [{},{},{}] ...

import React from "react"
import Card from "../components/Card"
    
export async function getStaticProps() {
    const res = await import("./db.json")
    return {
        props: {
            projects: res.projects,
        },
    }
}
const portfolio = ({ projects }) => {
    //this console.log works as expected
    console.log(projects)
    return (
        <div className='portfolio'>
            {projects.map((project, id) => (
                <Card key={id} props={project} /> 
            ))}
        </div>
    )
}
    
export default portfolio

试图破坏Props对象时,问题开始 。

import React, { useState } from "react"
import Image from "next/image"
    
const Card = ({ image, title, github, demo }) => {
    console.log(title)
    const [hovered, setHover] = useState(false)
    return (
        <div
            className='portfolio-card'
            onMouseEnter={() => setHover(!hovered)}
            onMouseLeave={() => setHover(!hovered)}
        >
            <div className='portfolio-card-info'>
                <h3 className='portfolio-card-title'>{title}</h3>
                {hovered && <p>ReactJs</p>}
                {hovered && <p>Recoil</p>}
                <ul className='portfolio-links'>
                    <li>
                        <a href={github}>Source</a>
                    </li>
                    <li>
                        <a href={demo}>Demo</a>
                    </li>
                </ul>
            </div>
            <Image className='portfolio-image' src={image} alt='portfolio-image' />
        </div>
    )
}
    
export default Card

传递给卡片组件 15:10如预期的那样

: > array(3)[{…},{…},{…}] portfolio:15:10
未定义的卡。JS:5:10

我检查了React Dev工具,例如,如果我选择了第一张卡,我可以看到道具。有人可以告诉我我在这里缺少什么现在这一天半了。

Goal: to map over 'portfolio' projects stored in a local json file, and pass each object to a card component i created.

The portfolio component is in the pages folder, and the console.log(line: 15 - correctly displays array of json objects // [ {}, {}, {} ]...

import React from "react"
import Card from "../components/Card"
    
export async function getStaticProps() {
    const res = await import("./db.json")
    return {
        props: {
            projects: res.projects,
        },
    }
}
const portfolio = ({ projects }) => {
    //this console.log works as expected
    console.log(projects)
    return (
        <div className='portfolio'>
            {projects.map((project, id) => (
                <Card key={id} props={project} /> 
            ))}
        </div>
    )
}
    
export default portfolio

the issue began when attempting to destructure the props object passed to the card component. The first thing i noticed were no images, but upon closer inspection: no title, no working links, etc. i receive undefined... and I'm not completely sure why.

import React, { useState } from "react"
import Image from "next/image"
    
const Card = ({ image, title, github, demo }) => {
    console.log(title)
    const [hovered, setHover] = useState(false)
    return (
        <div
            className='portfolio-card'
            onMouseEnter={() => setHover(!hovered)}
            onMouseLeave={() => setHover(!hovered)}
        >
            <div className='portfolio-card-info'>
                <h3 className='portfolio-card-title'>{title}</h3>
                {hovered && <p>ReactJs</p>}
                {hovered && <p>Recoil</p>}
                <ul className='portfolio-links'>
                    <li>
                        <a href={github}>Source</a>
                    </li>
                    <li>
                        <a href={demo}>Demo</a>
                    </li>
                </ul>
            </div>
            <Image className='portfolio-image' src={image} alt='portfolio-image' />
        </div>
    )
}
    
export default Card

the console.log from portfolio: 15: 10 shows the array of objects as expected.

the console.log from card: 5:10 is undefined.

Array(3) [ {…}, {…}, {…} ] portfolio:15:10
undefined Card.js:5:10

I checked the react dev tools, if i select the first card for example, i can see the props.can anyone tell me what i'm missing here, i've been on this now for a day and a half.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文