使用外部 JSON 文件数据作为不同页面中登陆部分的道具的最佳方法是什么?

发布于 2025-01-09 20:46:29 字数 2211 浏览 1 评论 0原文

我是 ReactJS 的新手,我需要这个令人困惑的问题的答案。

我有一个登陆页面,我想在我的主页和联系页面中使用。我想要的是将外部 JSON 信息作为 props 发送到这些页面以及每次创建新页面时。

我有一个外部 JSON 文件,我想将其作为 props 添加到我的登陆页面文件中 这样做的最佳实践是什么,我应该在状态中保存并将其作为道具发送,还是直接作为道具发送

JSON 文件:

{

"landing page" : {
    "home": {
        "id":1,
        "image": "../media/video/Ai Motion5.mp4",
        "title" : "MyAkbar for IT consultant & Services",
        "description":"Boost up Your Works With our Services. My Incrediable Team is Here to Save Your Time and Money.",
        "buttonOne": "Get A Demo"
    },
    "Contact" : {
        "id":2,
        "image": "../media/video/Ai Motion5.mp4",
        "title" : "Contact",
        "description":"sdadasdskdjaskljdas Team is Here to Save Your Time and Money.",
        "buttonOne": "Get A Demo"
    }
}

}

家庭文件:

import React, { Component } from 'react'
import LandingPage from "./landingPage/LandingPage"
import WaveSection from './waveSection/WaveSection'
import MyReview from "./reviewSection/MyReview"
import './styles/style.css'
import data from '../../json/data.json';

class Home extends Component{


    render(){
        return(
            <div id='home' className='home'>
                <LandingPage 
                    title = {data['landing page'].home.title} 
                    img = {data['landing page'].home.image}
                    description ={data['landing page'].home.description}
                    btn = {data['landing page'].home.buttonOne}
                />
                <WaveSection/>
                <MyReview/>
            </div>
        )
    }
}

export default Home

联系文件:

import React, { Component } from 'react'
import video from '../../media/video/Ai Motion.mp4';
class Contact extends Component{
    render(){
        return(
            <section className='contact-section landingPage-section'>
                <div className="container">
            <video  autoPlay muted loop="True" id='myVideo' src={video}></video>
                </div>
            </section>
        )
    }
}

export default Contact

I am new to reactJS and I need an answer for this confusing problem.

I have a landing page that I want to use in my home and contact page. What I want is to send external JSON info as props to these pages and every time I create new page.

I have an external JSON file and I want to add it as a props to my landing page file
What is the best practice to do so, should I save within a state and send it as a props or send it directly as a props

JSON File:

{

"landing page" : {
    "home": {
        "id":1,
        "image": "../media/video/Ai Motion5.mp4",
        "title" : "MyAkbar for IT consultant & Services",
        "description":"Boost up Your Works With our Services. My Incrediable Team is Here to Save Your Time and Money.",
        "buttonOne": "Get A Demo"
    },
    "Contact" : {
        "id":2,
        "image": "../media/video/Ai Motion5.mp4",
        "title" : "Contact",
        "description":"sdadasdskdjaskljdas Team is Here to Save Your Time and Money.",
        "buttonOne": "Get A Demo"
    }
}

}

Home file:

import React, { Component } from 'react'
import LandingPage from "./landingPage/LandingPage"
import WaveSection from './waveSection/WaveSection'
import MyReview from "./reviewSection/MyReview"
import './styles/style.css'
import data from '../../json/data.json';

class Home extends Component{


    render(){
        return(
            <div id='home' className='home'>
                <LandingPage 
                    title = {data['landing page'].home.title} 
                    img = {data['landing page'].home.image}
                    description ={data['landing page'].home.description}
                    btn = {data['landing page'].home.buttonOne}
                />
                <WaveSection/>
                <MyReview/>
            </div>
        )
    }
}

export default Home

Contact File:

import React, { Component } from 'react'
import video from '../../media/video/Ai Motion.mp4';
class Contact extends Component{
    render(){
        return(
            <section className='contact-section landingPage-section'>
                <div className="container">
            <video  autoPlay muted loop="True" id='myVideo' src={video}></video>
                </div>
            </section>
        )
    }
}

export default Contact

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2025-01-16 20:46:29

我将选择第一个选项(不将其存储在状态中),因为该数据是静态的并且应用程序不会直接修改它。

I will go with the first option (not storing it in state) as this data is static and the app does not modify it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文