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