繁体   English   中英

条件渲染在 reactjs 中不一致?

[英]conditional rendering not acting uniformly in reactjs?

我过去使用过条件渲染,但出于某种原因,它在这里处理一个元素而不是另一个元素,尽管它们都使用相同的 JSON 措辞和相同类型的条件渲染?

JSON 数组

//Adobe组件数据

export default[
    {
        workName: 'Switch up your "Grub" Motiongraphic',
        workDescription: 'This is a motion graphic comparing the environmental and health effects of consuming animal products compared to insects as dietary source of protein.',
        workTech: ['Adobe After effects'],
        videoAddress: ['https://youtu.be/cloat51hzDY'],
        Images: []
    },

    {
        workName: 'Simplyfit concept poster',
         workDescription: 'This is a poster developed explaining the concept of Simplyfit, a fitness application developed as part of my final year project.',
        workTech: ['Adobe Illustrator'],
        videoAddress: [],
        Images: ['SFPoster.jpg'],
    },

    {
        workName: 'Switch up your "Grub" Infographic',
         workDescription: 'This is an infographic developed explaining the benefits of consuming insects as a source of protein.',
        workTech: ['Adobe Illustrator'],
        videoAddress: [],
        Images: ['insectMotiongraphic.png'],
    },

    {
        workName: 'Crunchy Nut Advert spoof',
         workDescription: 'This video was made as a comedic spoof of a Crunchy Nut advert',
        workTech: ['Adobe Premier Pro'],
        videoAddress: ['https://youtu.be/y8S2RUYrLN8'],
        Images: [],
    },

    {
        workName: 'Icons and Designs',
        workDescription: 'These are a selection of logos and icons created by me.',
        workTech: ['Adobe Premier Pro'],
        videoAddress: [],
        Images: ['Mylogo.png'],
    },
    
]

我遇到的问题是 'videoAdress' 和 'Images' 我试过为它们设置 null 值 undefined 等但是对于图像,唯一阻止它们渲染的是将值设置为 [] 但是这个对 videoAdress 不起作用 iframe 仍然呈现?

反应js代码

{Data.map((Projects, index) => {
                        return <div className='Cards'>
                            <Carousel showThumbs={false} infiniteLoop={true} swipeable={false} emulateTouch={false} showStatus={false} autoPlay={slideShow} dynamicHeight={false}>
                                {Projects.Images && Projects.Images.map((Image, index) => { return <div className='image-iframeContainer'><img src={require("../assets/Port-images/Adobe/" + Image)} /></div> })}
                                {Projects.videoAddress && <div className='image-iframeContainer'><ReactPlayer url={Projects.videoAddress} muted={false} controls={false} onPlay={autoplayChange} onPause={autoplayChange} onEnded={autoplayChange} /></div>}
                            </Carousel>
                            {Projects.webAddress && <div className='webButton'><LinkIcon onClick=  { () => {window.open(Projects.webAddress);}}/></div>}
                            <h1>{Projects.workName}</h1>
                            {Projects.workTech.map((Tech, index) => { return <p className='techList'>{Tech}</p> })}
                            <div className='descriptionContainer'>
                                <p className='description'>{Projects.workDescription}</p>
                            </div>
                        </div>
                    })}

我想要的 function 是为了仅在有存储地址的情况下呈现图像和视频我确定我遗漏了一些非常愚蠢的东西但是我仍然坚持了一段时间。

条件渲染通过将条件转换为真值来工作。 例如:

Projects.Images && <Component />

等于:

!!Project.Images && <Component />

现在,如果您对空数组 [] 执行此操作,则真实值为TRUE 这意味着<ReactPlayer />使用 [] 值渲染,而<img />不渲染,因为 [].map() 不在空数组上运行。

要解决此问题,请执行以下操作:

{Projects.Images && Projects.Images.length > 0 && Projects.Images.map((Image, index) => {
  return <div className='image-iframeContainer'>
    <img src={require("../assets/Port-images/Adobe/" + Image)} />
  </div>
})} 

{Projects.videoAddress && Projects.videoAddress.length > 0 &&
<div className='image-iframeContainer'>
  <ReactPlayer url={Projects.videoAddress[0]} muted={false} controls={false} onPlay={autoplayChange} onPause={autoplayChange} onEnded={autoplayChange} />
</div>}

我注意到您的 videoAddress 没有使用 map() 方法,但我想这是一个错字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM