简体   繁体   English

Babel 期望“,”应该有一个“.”?

[英]Babel expects “,” where there should be a “.”?

So, the following implementation works just fine to read JSON data and turn it into rendered components - until I try to add the children.因此,以下实现可以很好地读取 JSON 数据并将其转换为渲染组件 - 直到我尝试添加孩子。 Then, it spits out an error.然后,它吐出一个错误。

function: function:

const catalogRenderer = (config) => {
  if (typeof KeysToComponentMap[config.component] !== "undefined") {
    return React.createElement(
      KeysToComponentMap[config.component],
      {
        key: config.key,
        title: config.title
      },
      {
        config.children && config.children.map(c => catalogRenderer(c))
      }
    );
  }
}

error:错误:

app.js:134 Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js)
"...Scripts/CatalogRenderer.js: Unexpected token, expected "," (25:14)"

console:安慰:

 },
  24 |       {
> 25 |         config.children && config.children.map(c => catalogRenderer(c))
     |               ^
  26 |       }
  27 |     );
  28 |   }

I'm using react as part of an electron application, it's a long story about all the moving parts, but everything else so far has worked just fine.我正在使用 react 作为 electron 应用程序的一部分,这是一个关于所有移动部件的长篇故事,但到目前为止其他一切都运行良好。 In the editor, if I move to the preceding { from that mysteriously disliked.在编辑器中,如果我从那个神秘的不喜欢移动到前面的 {。 on line 25, it's highlighting the period as if this should somehow close the bracket.在第 25 行,它突出显示了句点,好像这应该以某种方式关闭括号。

Is there something I'm not understanding about the syntax here?这里的语法有什么我不理解的地方吗? The same thing happens if I attempt to just map and render the children like so:如果我尝试只使用 map 并像这样渲染孩子,也会发生同样的事情:

      {
        config.children.map(c => catalogRenderer(c))
      }

I've tried enclosing the whole statement in brackets, curly braces, parentheses--no matter what I do, babel seems to expect a comma, but giving it a comma obviously doesn't help.我尝试将整个语句括在方括号、花括号、圆括号中——无论我做什么,babel 似乎都需要一个逗号,但给它一个逗号显然没有帮助。 Any idea what I'm doing wrong?知道我做错了什么吗?

eta: This is the JSON object I'm attempting to render from: eta:这是 JSON object 我试图从:

    const catConfig = {
      catalog: [
        {
          component: 'pen',
          title: `B.C. Palmer`,
          key: `B.C.PalmerPen`,
          children: `A child string`
        },
        {
          component: 'content',
          key: `B.C.PalmerWorldList`,
          children: [
            {
              component: 'world',
              title: `Rismere`,
              key: `RismereWorld`
            },
            {
              component: 'content',
              key: `RismereSeries`,
              children: [
                {
                  component: 'series',
                  title: `The Eidolon War`,
                  key: `TheEidolonWarSeries`
                },
                {
                  component: 'content',
                  key: `TheEidolonWarBooks`,
                  children: [
                    {
                      component: 'book',
                      title: `Magic's Heart`,
                      key: `MagicsHeartBook`
                    },
                    {
                      component: 'book',
                      title: `Magic's Fury`,
                      key: `MagicsFuryBook`
                    },
                    {
                      component: 'book',
                      title: `Magic's Grace`,
                      key: `MagicsGraceBook`
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          component: 'pen',
          title: `Simon Strange`,
          key: `SimonStrangePen`
        }
      ]
    }

This JSON will be generated via a database call, and written each time the database is updated, and update the state of the 'catalog' component.这个 JSON 将通过数据库调用生成,并在每次更新数据库时写入,并更新“目录”组件的 state。

So, for example, the second object in the catalog array above is a container which, when the first 'pen' component is clicked, becomes visible and shows a list of 'world' components (in this case, just the one.) However, the function only successfully renders any 'parent' components--if I take out the curly braces at lines 24 and 26, it simply skips them but doesn't error.因此,例如,上面目录数组中的第二个 object 是一个容器,当单击第一个“笔”组件时,它变得可见并显示“世界”组件列表(在这种情况下,只有一个。)但是,function 只能成功渲染任何“父”组件——如果我在第 24 行和第 26 行取出花括号,它只会跳过它们但不会出错。

The components are composed of button elements and a div (content).组件由按钮元素和一个 div(内容)组成。 The buttons will likely become Link element when I get this working, but the original version was written in vanilla javascript, I haven't implemented routing with the catalog yet.当我开始工作时,按钮可能会成为链接元素,但原始版本是用 vanilla javascript 编写的,我还没有使用目录实现路由。 So, the pen component for example:因此,以笔组件为例:

import React from 'react'

export default penButton => {
  return(
    <button className="catalogItem pen">
      <img src="src/icons/catPenName.png" className="catalogIcon"/>
      <p className="contentLabel">{penButton.title}</p>
    </button>
  )
}

Is a top level component, and gets rendered just fine.是一个顶级组件,并且渲染得很好。 It's next sibling (and the next sibling of any button except a book) is content:它的下一个兄弟(以及除一本书之外的任何按钮的下一个兄弟)是内容:

import React from 'react'

export default contentList => {
  return(
    <div className="contentList">
    </div>
  )
}

contentList is just a div with the contentList class, which handles visibility and animation. contentList 只是一个带有 contentList class 的 div,它处理可见性和 animation。 Should I have a place for the "children" key in JSON to populate the children of content?我应该在 JSON 中为“children”键留一个位置来填充内容的孩子吗?

When you want to render multipile children elemen'ts into your react component, you need to pass each child as a seperate parameter.当您想将多个子元素渲染到您的反应组件中时,您需要将每个子元素作为单独的参数传递。

See this answer as an example: how to render multiple children without JSX以这个答案为例: how to render multiple children without JSX

so your solution should be to use spread syntax .所以你的解决方案应该是使用传播语法

here is an example:这是一个例子:

const catalogRenderer = (config) => {
  if (typeof KeysToComponentMap[config.component] !== "undefined") {
    let childs = [];
    if (config.children) {
      childs = config.children.map(c => catalogRenderer(c));
    } 
    return React.createElement(
      KeysToComponentMap[config.component],
      {
        key: config.key,
        title: config.title
      },
      ...childs
    );
  }
}

Well, that was simple and a little silly.嗯,这很简单而且有点傻。 I updated the content component to:我将内容组件更新为:

import React from 'react'

export default contentList => {
  return(
    <div className="contentList">
      {contentList.children} <---- added
    </div>
  )
}

Content didn't have a place to put children.内容没有放置孩子的地方。 Obviously.明显地。

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

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