我遇到问题,当我使用componentDidMount()
时,componentDidMount()
用于在使用函数_getContentTooltip()
时显示工具提示,然后问题显示错误common.js:444 RangeError: Maximum call stack size exceeded
import ZCommon from 'utils/common';
import React from 'react';
import ReactDOM from 'react-dom';
class TooltipUtil extends React.Component {
constructor(props) {
super(props);
this.state = {
guidesContent: [],
zguides: [],
current: 1,
hidden: 0
};
}
shouldComponentUpdate(nextProps, nextState) {
return (
this.state.current !== nextState.current ||
this.state.zguides !== nextState.zguides
);
}
_storageData() {
this.state.guidesContent = [{
"id":"1",
"description": "Đây là title của người chat or group",
"name":"tabmsg.searchicon",
"title": "abc",
}, {
"id":"2",
"name":"tabmsg.creategroup",
"description": "Bạn click vào đây để tạo nhóm",
"title": "Xưu nhân",
}, {
"id":"3",
"name":"csc.header.search",
"description": "Đây là khung để nhập nội dung chat",
"title": "abc",
}];
this.setState({
guidesContent: this.state.guidesContent.sort(function(a,b){ return a.id > b.id } )
});
return this.state.guidesContent;
}
_getContentTooltip(){
// this.serverRequest.abort();
let current= this.state.current;
let _guides = this._storageData();
let ele = document.querySelectorAll(".tooltip");
for (var i = 0; i < ele.length; i++) {
var no = ele[i].getAttribute('data-tooltip');
let Tcontent = Object.keys(_guides).filter(function(key) {
if(_guides[key].name == no){
if(_guides[key].id == current){
return key;
}
}
});
this.setState({
zguides: this.state.guidesContent[Tcontent]
})
}
}
componentDidMount(){
this._getContentTooltip();
}
componentDidUpdate(){
this.componentDidMount();
}
_handlerClickClose() {
let _guides = this._storageData();
if(this.state.current <= _guides.length ){
this.setState({
current: this.state.current + 1
});
}
}
render() {
let guides = null;
let obj = this.state.zguides;
let show = this.state.zguides != undefined ? "show" : ' ';
console.log(this.state.zguides);
guides = (
<div className={'guide ' + show } style={{ width:'200px',left:'0'}}>
<div className="guide-content flx">
<div href="#" className="fa fa-close" onClick= {this._handlerClickClose.bind(this)}></div>
<h4>{this.state.zguides['title']}</h4>
<p>{this.state.zguides['description']}</p>
</div>
</div>
);
return guides;
}
_handlerClickClose() {
let _guides = this._storageData();
if(this.state.current <= _guides.length ){
this.setState({
current: this.state.current + 1
});
}
}
}
export default TooltipUtil;
答案 0 :(得分:0)
在 _getContentTooltip()函数中,您正在更改state.which导致组件更新,因此运行 componentDidUpdate()函数,这也会再次调用 componentDidMount( )即可。该函数再次调用 getContentTooltip() .so注释以下行
componentDidUpdate(){
//this.componentDidMount();
}
答案 1 :(得分:0)
所有你不应该强迫生命周期函数强制调用。现在出现最大堆栈大小错误,因为_getContentTooltip
正在设置状态,因此将触发重新生成,从而导致调用componentDidUpdate生命周期函数,您再次调用componentDidMount,从而进入无限循环。您可以说已检查shouldComponentUpdate
中的先前和当前状态值是否相等,但比较this.state.zguides !== nextState.zguides
之类的数组将返回true。
看到这个答案:
How to compare arrays in JavaScript?
如果要定期触发_getContentTooltip
,请在componentDidMount中的setInterval函数中调用它
class TooltipUtil extends React.Component {
constructor(props) {
super(props);
this.state = {
guidesContent: [],
zguides: [],
current: 1,
hidden: 0
};
}
shouldComponentUpdate(nextProps, nextState) {
return (
this.state.current !== nextState.current ||
!this.state.zguides.equals(nextState.zguides)
);
}
_storageData() {
this.state.guidesContent = [{
"id":"1",
"description": "Đây là title của người chat or group",
"name":"tabmsg.searchicon",
"title": "abc",
}, {
"id":"2",
"name":"tabmsg.creategroup",
"description": "Bạn click vào đây để tạo nhóm",
"title": "Xưu nhân",
}, {
"id":"3",
"name":"csc.header.search",
"description": "Đây là khung để nhập nội dung chat",
"title": "abc",
}];
this.setState({
guidesContent: this.state.guidesContent.sort(function(a,b){ return a.id > b.id } )
});
return this.state.guidesContent;
}
_getContentTooltip(){
// this.serverRequest.abort();
let current= this.state.current;
let _guides = this._storageData();
let ele = document.querySelectorAll(".tooltip");
for (var i = 0; i < ele.length; i++) {
var no = ele[i].getAttribute('data-tooltip');
let Tcontent = Object.keys(_guides).filter(function(key) {
if(_guides[key].name == no){
if(_guides[key].id == current){
return key;
}
}
});
this.setState({
zguides: this.state.guidesContent[Tcontent]
})
}
}
componentDidMount(){
setInterval(() => {
this._getContentTooltip();
}, 1000)
}
_handlerClickClose() {
let _guides = this._storageData();
if(this.state.current <= _guides.length ){
this.setState({
current: this.state.current + 1
});
}
}
render() {
let guides = null;
let obj = this.state.zguides;
let show = this.state.zguides != undefined ? "show" : ' ';
console.log(this.state.zguides);
guides = (
<div className={'guide ' + show } style={{ width:'200px',left:'0'}}>
<div className="guide-content flx">
<div href="#" className="fa fa-close" onClick= {this._handlerClickClose.bind(this)}></div>
<h4>{this.state.zguides['title']}</h4>
<p>{this.state.zguides['description']}</p>
</div>
</div>
);
return guides;
}
_handlerClickClose() {
let _guides = this._storageData();
if(this.state.current <= _guides.length ){
this.setState({
current: this.state.current + 1
});
}
}
}
export default TooltipUtil;