Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在render not working中有条件地添加类-React/Gatsby_Javascript_Reactjs_Gatsby - Fatal编程技术网

Javascript 在render not working中有条件地添加类-React/Gatsby

Javascript 在render not working中有条件地添加类-React/Gatsby,javascript,reactjs,gatsby,Javascript,Reactjs,Gatsby,我有这个部分: const imageDisplay = false; ... <PostsGrid posts={filteredPosts} defaultCoverImage={defaultCoverImage} imageDisplay={imageDisplay} /> 这是因为运算符具有优先级。你应该用括号。请看以下内容: imageDisplay=false console.log('cover-ima

我有这个部分:

const imageDisplay = false;
  ...
    <PostsGrid
       posts={filteredPosts}
       defaultCoverImage={defaultCoverImage}
       imageDisplay={imageDisplay}
    />

这是因为运算符具有优先级。你应该用括号。请看以下内容:

imageDisplay=false
console.log('cover-image'+!imageDisplay?'hide':'show')
imageDisplay=true
console.log('cover-image'+!imageDisplay?'hide':'show')
图像显示=假
console.log('cover-image'+(!imageDisplay?'hide':'show'))
imageDisplay=true

console.log('cover-image'+(imageDisplay?'hide':'show'))
其原因是运算符的优先级。你应该用括号。请看以下内容:

imageDisplay=false
console.log('cover-image'+!imageDisplay?'hide':'show')
imageDisplay=true
console.log('cover-image'+!imageDisplay?'hide':'show')
图像显示=假
console.log('cover-image'+(!imageDisplay?'hide':'show'))
imageDisplay=true

log('cover-image'+(imageDisplay?'hide':'show'))
我建议您在示例中习惯字符串插值,您可以这样做

className={'cover image${imageDisplay?show':'hide'}}


您的示例是否应该将这两个类放在一起,从而导致无效的“cover imageshow”或“cover imagehide”?也许这就是导致错误的原因

我建议您在示例中习惯字符串插值,您可以这样做

className={'cover image${imageDisplay?show':'hide'}}

您的示例是否应该将这两个类放在一起,从而导致无效的“cover imageshow”或“cover imagehide”?可能是这个问题导致了错误

我发誓我以前试过(),但我的问题不仅仅是封面图片后面没有空格我发誓我以前试过(),我的问题不仅仅是封面图片后面没有空格
...
console.log(imageDisplay)// prints false

    <Img
         className={'cover-image' + imageDisplay ? 'show' : 'hide'}
         fluid={
                post.coverImage
                ? post.coverImage.fluid
                : defaultCoverImage.fluid
                }
                alt={post.title}
          />
className={'cover-image' + !imageDisplay ? 'hide' : 'show'}