首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >意外的记号。您的意思是‘{’}}‘或’&rbrace错误时试图呈现

意外的记号。您的意思是‘{’}}‘或’&rbrace错误时试图呈现
EN

Stack Overflow用户
提问于 2022-08-21 15:25:02
回答 3查看 127关注 0票数 0

这里是我在将内容呈现给html时试图传递的map函数,但是我在条件运算符的第3行中得到了, expected错误。在结束括号上,我得到了Unexpected token. Did you mean {‘}}or},为什么会这样呢?是因为语法原因吗?

代码语言:javascript
复制
 {newMessages.map(function (item) {
                         return (
                          { userId === item.event.sender ?
                            <div class="flex justify-start mb-4">
                              <div
                                class="ml-2 py-3 px-4 bg-blue-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white"
                              >
                                {item.event.content.body}
                              </div>
                              <img
                                src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
                                class="object-cover h-8 w-8 rounded-full"
                                alt=""
                              />
                            </div>
                            :
                            <div class="flex justify-end mb-4">
                              <img
                                src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
                                class="object-cover h-8 w-8 rounded-full"
                                alt=""
                              />
                              <div
                                class="ml-2 py-3 px-4 bg-gray-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white"
                              >
                                {item.event.content.body}
                              </div>
                            </div>}                     
                            )
                      }
                    )
                    }
EN

回答 3

Stack Overflow用户

发布于 2022-08-21 15:29:28

在将一个{表达式内插到JSX中时,应该使用{--例如,<div>{someVariable}</div>。如果您不在JSX的上下文中,则使用{来分隔对象文字的开头。

由于您不在JSX上下文中,所以此时:

代码语言:javascript
复制
return (
    { userId === item.event.sender ?
    ^

抛出一个错误。

在这里,您可以完全删除{并返回元素数组,而不需要在该级别上使用任何JSX。

代码语言:javascript
复制
{newMessages.map(function (item) {
  return (
    userId === item.event.sender ?

如果每个映射项周围都有一些元素,例如:

代码语言:javascript
复制
{newMessages.map(function (item) {
  return (
    <div className="item-container">
      {
        userId === item.event.sender ?
票数 0
EN

Stack Overflow用户

发布于 2022-08-21 15:33:45

下面的代码应该可以工作

代码语言:javascript
复制
{
    newMessages.map(function (item) {
        return userId === item.event.sender ? (
            <div className="flex justify-start mb-4">
                <div className="ml-2 py-3 px-4 bg-blue-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white">
                    {item.event.content.body}
                </div>
                <img
                    src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
                    className="object-cover h-8 w-8 rounded-full"
                    alt=""
                />
            </div>
        ) : (
            <div className="flex justify-end mb-4">
                <img
                    src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
                    className="object-cover h-8 w-8 rounded-full"
                    alt=""
                />
                <div className="ml-2 py-3 px-4 bg-gray-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white">
                    {item.event.content.body}
                </div>
            </div>
        );
    });
}

你用的是额外的花括号

票数 0
EN

Stack Overflow用户

发布于 2022-08-21 15:35:10

你有一些语法错误。只需确保用圆括号()括住每个JSX块,并使用className属性而不是class,因为它是JavaScript中的保留关键字。

代码语言:javascript
复制
{newMessages.map((item) =>
  userId === item.event.sender ? (
    <div className="flex justify-start mb-4">
      <div className="ml-2 py-3 px-4 bg-blue-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white">
        {item.event.content.body}
      </div>
      <img
        src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
        className="object-cover h-8 w-8 rounded-full"
        alt=""
      />
    </div>
  ) : (
    <div className="flex justify-end mb-4">
      <img
        src="https://source.unsplash.com/vpOeXr5wmR4/600x600"
        className="object-cover h-8 w-8 rounded-full"
        alt=""
      />
      <div className="ml-2 py-3 px-4 bg-gray-400 rounded-br-3xl rounded-tr-3xl rounded-tl-xl text-white">
        {item.event.content.body}
      </div>
    </div>
  )
)}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73435764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档