【问题标题】:how to access refs array element如何访问 refs 数组元素
【发布时间】:2020-08-04 18:19:54
【问题描述】:

我的课程是标签容器。显示每个选项卡的 li 元素。我正在使用 refs 数组来识别列表元素,并通过添加和删除“tab-list-active”类来设置和取消设置活动选项卡。我正在使用 react 版本 16.13.1

标签

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);

当我单击列表选项卡时出现错误,我收到错误。我不明白如何使用 refs 数组为特定索引添加和删除类。

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

【问题讨论】:

  • 你不应该在这里使用 refs;只需将activeTab 存储在您的状态中,然后根据它重新计算className

标签: javascript reactjs aem


【解决方案1】:

正如我的评论所说:不要使用引用,只需将活动选项卡作为选项卡组件状态的一部分:

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>
    );
  }
}

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多