简体   繁体   English

如何访问 refs 数组元素

[英]how to access refs array element

My class is tabs container.我的 class 是标签容器。 the li element displaying each tab.I am using refs array to identify list elements and to set and unset the active tab by adding and removing 'tab-list-active' class.I am using react version 16.13.1显示每个选项卡的 li 元素。我正在使用 refs 数组来识别列表元素,并通过添加和删除“tab-list-active”class 来设置和取消设置活动选项卡。我正在使用反应版本 16.13.1

Tab标签

import React, { createRef } from "react";
import {
  MapTo,
  withComponentMappingContext,
  ResponsiveGrid,
} from "@adobe/cq-react-editable-components";

import Tab from "./Tab";
require("./Tabs.css");

const TabsContainerEditConfig = {
  emptyLabel: "Tabs",

  isEmpty: function (props) {
    return !props;
  },
};

export default class TabsContainer extends ResponsiveGrid {
  activeTabRef = this.props.columns.map((x) => createRef(null));

  get containerProps() {
    let containerProps = super.containerProps;
    containerProps.className =
      (containerProps.className || "") + " aem-Grid " + this.props.gridClassNames;
    return containerProps;
  }

  render() {
    if (this.props.columns.length == 0 || !this.props.columns[0].title) {
      return (
        <div {...this.containerProps}>
          {super.childComponents}
          {super.placeholderComponent}
        </div>
      );
    } else {
      let activeTab = "";
      if (this.props.columns[0].title) activeTab = this.props.columns[0].title;

      return (
        <div className="tabs">
          <div {...this.containerProps}>
            <ol className="tab-list">
              {this.props.columns.map((child, id) => {
                let className = "tab-list-item";
                if (activeTab === child.id) {
                  className += " tab-list-active";
                }
                return (
                  <li
                    ref={(el) => (this.activeTabRef[child.id] = el)}
                    key={id}
                    onClick={() => {
                      let prevActiveTab = activeTab;
                      activeTab = child.id;
                      this.activeTabRef[prevActiveTab].current.classList.remove(
                        "tab-list-active",
                      );
                      this.activeTabRef[activeTab].current.classList.add("tab-list-active");
                    }}
                    className={className}
                  >
                    {child.title}
                  </li>
                );
              })}
            </ol>
            <div className="tab-content">
              {this.props.columns.map((child, id) => {
                if (child.id !== activeTab) return undefined;
                return (
                  <div id={child.id} key={id}>
                    {this.childComponents}
                    {this.placeholderComponent}
                  </div>
                );
              })}
            </div>
          </div>
        </div>
      );
    }
  }
}

MapTo("tabscontainer")(withComponentMappingContext(TabsContainer), TabsContainerEditConfig);

When I click on the list Tab I get error I am getting error.当我单击列表选项卡时,我收到错误消息。 I did not understand how to add and remove class using refs array for particular index.我不明白如何使用 refs 数组为特定索引添加和删除 class 。

Uncaught TypeError: Cannot read property 'classList' of undefined at onClick

As my comment says: don't use refs, just make the active tab a part of the Tabs component's state:正如我的评论所说:不要使用引用,只需将活动选项卡作为选项卡组件的 state 的一部分:

export default class TabsContainer extends ResponsiveGrid {
  constructor(props) {
    super(props);
    const activeTab =
      props.columns && props.columns[0] && props.columns[0].title
        ? props.columns[0].title
        : "";
    this.state = { activeTab };
  }

  get containerProps() {
    let containerProps = super.containerProps;
    containerProps.className =
      (containerProps.className || "") + " aem-Grid " + this.props.gridClassNames;
    return containerProps;
  }

  renderTabBar(columns, activeTab) {
    return (
      <ol className="tab-list">
        {columns.map((child, id) => {
          let className = "tab-list-item";
          if (activeTab === child.id) {
            className += " tab-list-active";
          }
          return (
            <li
              key={id}
              onClick={() => this.setState({ activeTab: child.id })}
              className={className}
            >
              {child.title}
            </li>
          );
        })}
      </ol>
    );
  }

  renderContent(columns, activeTab) {
    return (
      <div className="tab-content">
        {columns.map((child, id) => {
          if (child.id !== activeTab) return undefined;
          return (
            <div id={child.id} key={id}>
              {this.childComponents}
              {this.placeholderComponent}
            </div>
          );
        })}
      </div>
    );
  }

  render() {
    const { columns } = this.props;
    const { activeTab } = this.state;
    if (columns.length === 0 || !columns[0].title) {
      return (
        <div {...this.containerProps}>
          {this.childComponents}
          {this.placeholderComponent}
        </div>
      );
    }
    return (
      <div className="tabs">
        <div {...this.containerProps}>
          {this.renderTabBar(columns, activeTab)}
          {this.renderContent(columns, activeTab)}
        </div>
      </div>
    );
  }
}

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

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