Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

javascript - Maximum call stack size exceeded - Connected React Component

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

Maximum call stack size exceeded

When this code is run. If I comment out:

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

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>
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because of this loop:

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

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

Remove setState from getTabs method, it will work.

Another issue is here:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...