我尝试仅使用三元运算符将仅由用户创建的 firebase 数据返回到他们的主页,但我不断收到错误:语法错误:意外 token ,预期
谁能看出代码哪里出错了? 如果我将三元组包裹在删除按钮周围,它将起作用,只允许用户删除他们创建的项目。我只想显示他们创建的东西。 react 代码如下:
<ul>
{this.state.items.map((item) => {
return (
{item.user === this.state.user.displayName || item.user === this.state.user.email
? <li key={item.id}>
<h3>{item.topic}</h3>
<p>author: {item.user}
<button onClick={() => this.removeItem(item.id)}>Remove Item</button>
</p>
</li>
: null}
)
})}
</ul>
最佳答案
You can embed any JavaScript expression in JSX by wrapping it in curly braces.
{}
当我们想在 JSX 中放置一些 js 表达式时是必需的,在你的情况下它不是必需的。如果您使用 {}
,则表示您正在尝试返回一个对象。
这样写:
return (
item.user === this.state.user.displayName || item.user === this.state.user.email?
<li key={item.id}>
<h3>{item.topic}</h3>
<p>
author: {item.user}
<button onClick={() => this.removeItem(item.id)}>Remove Item</button>
</p>
</li>
: null
)
关于javascript - react 三元运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47020643/