Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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 在数组中“推送”JSX元素时响应变量求值_Javascript_Reactjs - Fatal编程技术网

Javascript 在数组中“推送”JSX元素时响应变量求值

Javascript 在数组中“推送”JSX元素时响应变量求值,javascript,reactjs,Javascript,Reactjs,我正在数组中推送JSX元素 for (category of this.state.categories) { categories.push( <li> <label className="tree-toggler nav-header list-group-item" onClick={() => this.showHideConnectorNames(category)} >

我正在数组中推送JSX元素

for (category of this.state.categories) {
  categories.push(
    <li>
      <label 
        className="tree-toggler nav-header list-group-item" 
        onClick={() => this.showHideConnectorNames(category)}
      >
        {category}
      </label>
      <ul 
        className="tree ul-no-style" 
        id={category+'Connectors'}
      >
      </ul>
    </li>
  );
}

这是一个React问题还是我做错了什么?

您正在错误地使用以防止错误地使用它,我想改用map:

const categories = this.state.categories.map(category => (
  // not sure what to use for key here but it's missing
  <li key={category.id}>
    <label
      className="tree-toggler nav-header list-group-item"
      onClick={() => this.showHideConnectorNames(category)}
    >
      {category}
    </label>
    <ul
      className="tree ul-no-style"
      id={category + 'Connectors'}
    ></ul>
  </li>
));

您正在错误地使用for of以防止错误地使用它我想改用map:

const categories = this.state.categories.map(category => (
  // not sure what to use for key here but it's missing
  <li key={category.id}>
    <label
      className="tree-toggler nav-header list-group-item"
      onClick={() => this.showHideConnectorNames(category)}
    >
      {category}
    </label>
    <ul
      className="tree ul-no-style"
      id={category + 'Connectors'}
    ></ul>
  </li>
));

我不确定构建阵列后要对其执行什么操作,但我认为使用map调用会更有效率。只要将其放在return语句中,即可呈现列表项:

<ul>
  {this.state.categories.map(cat => (
    // your li has a missing key property
    <li>
      <label
        className="tree-toggler nav-header list-group-item"
        onClick={() => this.showHideConnectorNames(cat)}
      >
        {cat}
      </label>
      <ul
        className="tree ul-no-style"
        id={cat + 'Connectors'}
      ></ul>
    </li>
  ))}
</ul>

我不确定构建阵列后要对其执行什么操作,但我认为使用map调用会更有效率。只要将其放在return语句中,即可呈现列表项:

<ul>
  {this.state.categories.map(cat => (
    // your li has a missing key property
    <li>
      <label
        className="tree-toggler nav-header list-group-item"
        onClick={() => this.showHideConnectorNames(cat)}
      >
        {cat}
      </label>
      <ul
        className="tree ul-no-style"
        id={cat + 'Connectors'}
      ></ul>
    </li>
  ))}
</ul>

只需添加let,那么类别将在范围内是本地的


对于这个.state.categories的let类别,只需添加let,那么类别将在作用域的内部是本地的


对于这个.state.categories的let类,您可以发布整个组件吗?我认为您的条件语句缺少括号{}。如果{…}你能发布你的整个组件吗?我认为你的条件语句缺少括号{}。如果{…}是的,这也会解决它,因为您创建了一个新的category变量,但我更喜欢映射this.state.categories。这是两个不同的问题:当然,在react应用程序中,映射要正确得多,但只是为了指出一个范围误解,我编写了这个解决方案yes,这也会解决问题,因为您创建了一个新的category变量,但我更喜欢映射this.state.categories。这是两个不同的问题:当然,在react应用程序中,映射更为正确,但只是为了指出我编写此解决方案时存在的范围误解