简体   繁体   English

条件渲染在 reactjs 中不一致?

[英]conditional rendering not acting uniformly in reactjs?

I have used conditional rendering in the past but for some reason, it's working on one element here but not on another although they are both using the same JSON phrasing and the same type of conditional rendering?我过去使用过条件渲染,但出于某种原因,它在这里处理一个元素而不是另一个元素,尽管它们都使用相同的 JSON 措辞和相同类型的条件渲染?

JSON array JSON 数组

//Adobe component data //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'],
    },
    
]

The problem I'm having is with the 'videoAdress' and the 'Images' I've tried setting null values undefined etc for them both but for the images the only thing that stops them from rendering is setting the value as [] but this doesn't work for the videoAdress the iframe is still rendered?我遇到的问题是 'videoAdress' 和 'Images' 我试过为它们设置 null 值 undefined 等但是对于图像,唯一阻止它们渲染的是将值设置为 [] 但是这个对 videoAdress 不起作用 iframe 仍然呈现?

React js code反应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>
                    })}

The function I would like is for the Images and Videos only to render if there is a stored address I'm sure I'm missing something very silly but still, I've been stuck on this for awhile.我想要的 function 是为了仅在有存储地址的情况下呈现图像和视频我确定我遗漏了一些非常愚蠢的东西但是我仍然坚持了一段时间。

Conditional rendering works by casting the condition to a truthy value.条件渲染通过将条件转换为真值来工作。 For example:例如:

Projects.Images && <Component />

is equal to this:等于:

!!Project.Images && <Component />

Now if you do this for an empty array [], the truthy value is TRUE .现在,如果您对空数组 [] 执行此操作,则真实值为TRUE This means that <ReactPlayer /> is rendered with [] value and the <img /> is not rendered because [].map() doesn't run on an empty array.这意味着<ReactPlayer />使用 [] 值渲染,而<img />不渲染,因为 [].map() 不在空数组上运行。

To fix this do this instead:要解决此问题,请执行以下操作:

{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>}

I noticed that your videoAddress doesn't use the map() method, but I guess it's a typo我注意到您的 videoAddress 没有使用 map() 方法,但我想这是一个错字

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

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