本文介绍了超出最大调用堆栈大小 - 连接的React组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活弄清楚为什么我会收到错误:

I can't for the life of me figure out why I'm getting error:

运行此代码时。如果我注释掉:

When this code is run. If I comment out:

const tabs = this.getTabs(breakpoints,panels,selectedTab);

错误消失了。我甚至已经注释掉了其他 setState()调用来尝试缩小问题所在的位置。

the error goes away. I have even commented out other setState() calls to try and narrow down where the problem was at.

代码(删除了额外的函数):

Code (removed the extra functions):

export default class SearchTabs extends Component {
  constructor() {
    super();
    this.state = {
      filters: null,
      filter: null,
      isDropdownOpen: false,
      selectedFilter: null,
    };

    this.getTabs = this.getTabs.bind(this);
    this.tabChanged = this.tabChanged.bind(this);
    this.setSelectedFilter = this.setSelectedFilter.bind(this);

    this.closeDropdown = this.closeDropdown.bind(this);
    this.openDropdown = this.openDropdown.bind(this);
  }

  componentDidMount() {
    const { panels } = this.props;
    if (!panels || !panels.members || panels.members.length === 0) {
      this.props.fetchSearch();
    }
  }


  getTabs(breakpoints, panels, selectedTab) {
    const tabs = panels.member.map((panel, idx) => {
      const { id: panelId, headline } = panel;
      const url = getHeaderLogo(panel, 50);
      const item = url ? <img src={url} alt={headline} /> : headline;

      const classname = classNames([
        searchResultsTheme.tabItem,
        (idx === selectedTab) ? searchResultsTheme.active : null,
      ]);

      this.setState({ filter: this.renderFilters(
        panel,
        breakpoints,
        this.setSelectedFilter,
        this.state.selectedFilter,
        this.state.isDropdownOpen,
      ) || null });

      return (
        <TabItem
          key={panelId}
          classname={`${classname} search-tab`}
          headline={headline}
          idx={idx}
          content={item}
          onclick={this.tabChanged(idx, headline)}
        />
      );
    });

    return tabs;
  }

  render() {
    const { panels, selectedTab } = this.props;

    if (!panels || panels.length === 0) return null;

    const tabs = this.getTabs(breakpoints, panels, selectedTab);

    return (
      <div className={searchResultsTheme.filters}>
        <ul className={`${searchResultsTheme.tabs} ft-search-tabs`}>{tabs}</ul>
        <div className={searchResultsTheme.dropdown}>{this.state.filter}</div>
      </div>
    );
  }
}

export const TabItem = ({ classname, content, onclick, key }) => (
  <li key={key} className={`${classname} tab-item`} onClick={onclick} >{content}</li>
);


推荐答案

由于这个循环:

  render  ----->   getTabs  ----->  setState -----
    ^                                            |
    |                                            |
    |____________________________________________v

您正在从渲染中调用getTabs方法,并执行 setState 其中, setState 将触发重新渲染,再次getTabs ..... 无限循环

You are calling getTabs method from render, and doing setState inside that, setState will trigger re-rendering, again getTabs ..... Infinite loop.

getTabs 方法中删除 setState ,它会起作用。

Remove setState from getTabs method, it will work.

另一个问题是:

onclick={this.tabChanged(idx, headline)}

我们需要为onClick事件分配一个函数,我们不需要要调用它,但是在这里你调用那个方法,使用它:

We need to assign a function to onClick event, we don't need to call it, but here you are calling that method, use this:

onclick={() => this.tabChanged(idx, headline)}

这篇关于超出最大调用堆栈大小 - 连接的React组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 06:14