我不能为我的生活弄清楚为什么我会收到错误:
超出最大调用堆栈大小
运行此代码时。如果我发表评论:
const tabs = this.getTabs(breakpoints, panels, selectedTab);
错误消失了。我甚至评论过其他setState()
次电话,试图缩小问题所在的范围。
代码(删除了额外的功能):
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>
);
答案 0 :(得分:5)
由于这个循环:
render -----> getTabs -----> setState -----
^ |
| |
|____________________________________________v
你正在从渲染中调用getTabs方法,并在其中执行setState
,setState
将触发重新渲染,再次getTabs ..... 无限循环。< / p>
从setState
方法移除getTabs
,它会起作用。
另一个问题是:
onclick={this.tabChanged(idx, headline)}
我们需要为onClick事件分配一个函数,我们不需要调用它,但是你在这里调用那个方法,使用它:
onclick={() => this.tabChanged(idx, headline)}